简体   繁体   English

使用 Matplotlib 设置 Y 轴的范围

[英]Set range for Y axis using Matplotlib

I need to plot some columns from DataFrame in two different graph using Matplotlib.我需要使用 Matplotlib 在两个不同的图表中绘制 DataFrame 中的一些列。 I'm be able to do this using the code below:我可以使用下面的代码来做到这一点:

df.plot(x=datetime_column_name, y='NBR', kind='line', ax=ax1, marker='o', markerfacecolor='black', markersize=6, color='lightgrey', linewidth=2)
df.plot(x=datetime_column_name, y='NDVI', kind='line', ax=ax1, marker='o', markerfacecolor='forestgreen', markersize=6, color='lightgreen', linewidth=2)
df.plot(x=datetime_column_name, y=temperature_column_name, kind='line', ax=ax2, marker='o', markerfacecolor='red', markersize=6, color='darkorange', linewidth=2)

ax1.set_title(f'Relation between NDVI and NBR.')
ax1.annotate(
    f'Wildfire event {ref_datetime}',
    xy=(event_date, 0.8),
    xytext=(0.4, 0.95),
    textcoords='axes fraction',
    arrowprops=dict(facecolor='yellow', shrink=0.05),
    horizontalalignment='right',
    verticalalignment='top'
)
ax1.xaxis.set_label_text('Sensing Date')
ax1.xaxis.set_major_locator(mdates.MonthLocator(interval=2))
ax1.xaxis.set_major_formatter(mdates.DateFormatter("%b %Y"))
ax1.yaxis.set_label_text('Index Value')
ax1.yaxis.ticker(formatter=[-0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1.0])

ax2......

plt.legend(loc='best')
plt.show()

The problems comes when I set a specific range for y axis:当我为 y 轴设置特定范围时,问题就来了:

AttributeError: 'YAxis' object has no attribute 'ticker' AttributeError:“YAxis”对象没有属性“ticker”

在此处输入图像描述

It is not clear for me how I can set the range;我不清楚如何设置范围; someone can help me?有人可以帮助我吗?

NB1 : I'm using ax1.yaxis.ticker(formatter=[-0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1.0]) for set the range NB1 :我正在使用ax1.yaxis.ticker(formatter=[-0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1.0])设置范围

NB2 : with the code below I can set my range on y-axis NB2 :使用下面的代码,我可以在 y 轴上设置我的范围

fig = plt.figure(figsize=(20, 20), dpi=80)
df.plot(x=datetime_column_name, y='NBR', kind='line', marker='o', markerfacecolor='black', markersize=6, color='gray', linewidth=2)
plt.yticks([-0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1.0])

在此处输入图像描述

我的问题的解决方案是:

ax1.set_yticks(ticks=[-0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1.0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM