简体   繁体   中英

Set range for Y axis using Matplotlib

I need to plot some columns from DataFrame in two different graph using Matplotlib. 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:

AttributeError: 'YAxis' object has no attribute '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

NB2 : with the code below I can set my range on y-axis

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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