簡體   English   中英

使用 Matplotlib 設置 Y 軸的范圍

[英]Set range for Y axis using Matplotlib

我需要使用 Matplotlib 在兩個不同的圖表中繪制 DataFrame 中的一些列。 我可以使用下面的代碼來做到這一點:

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

當我為 y 軸設置特定范圍時,問題就來了:

AttributeError:“YAxis”對象沒有屬性“ticker”

在此處輸入圖像描述

我不清楚如何設置范圍; 有人可以幫助我嗎?

NB1 :我正在使用ax1.yaxis.ticker(formatter=[-0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1.0])設置范圍

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