亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 matplotlib 中設置兩個時間格式化程序?

如何在 matplotlib 中設置兩個時間格式化程序?

ITMISS 2023-02-07 17:26:51
此圖表由 Excel 構建。我怎樣才能使用 matplotlib 做同樣的事情?我的意思是如何添加兩個格式化程序:年個月?,F在我使用這樣的東西:現在我使用這樣的東西:fig, ax = plt.subplots(1,1)ax.margins(x=0)ax.plot(list(df['Date']), list(df['Value']), color="g")ax.xaxis.set_major_locator(matplotlib.dates.YearLocator())ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%Y'))plt.text(df["Date"].iloc[-1], df["Value"].iloc[-1], df["Value"].iloc[-1])plt.title(title)plt.get_current_fig_manager().full_screen_toggle()plt.grid(axis = 'y')plt.savefig('pict\\'+sheet.cell_value(row,1).split(',')[0]+'.png', dpi=300, format='png')#plt.show()plt.close(fig)它繪制:
查看完整描述

1 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊

您應該為標簽使用輔助軸year,同時為標簽使用主要軸month。您可以使用以下內容生成輔助軸:


import matplotlib.pyplot as plt

from mpl_toolkits.axes_grid1 import host_subplot

import mpl_toolkits.axisartist as AA

import matplotlib.dates as md


fig = plt.figure()

months = host_subplot(111, axes_class = AA.Axes, figure = fig)

plt.subplots_adjust(bottom = 0.1)

years = months.twiny()

然后你應該將輔助軸移動到底部:


offset = -20

new_fixed_axis = years.get_grid_helper().new_fixed_axis

years.axis['bottom'] = new_fixed_axis(loc = 'bottom',

                                      axes = years,

                                      offset = (0, offset))

最后,您繪制然后使用 和 調整主軸和次軸md.MonthLocator格式md.YearLocator。這樣的事情應該沒問題:


months.xaxis.set_major_locator(md.MonthLocator(interval = 1))

months.xaxis.set_major_formatter(md.DateFormatter('%B'))

months.set_xlim([df['Date'].iloc[0], df['Date'].iloc[-1]])


years.xaxis.set_major_locator(md.YearLocator(interval = 1))

years.xaxis.set_major_formatter(md.DateFormatter('%H'))

years.set_xlim([df['Date'].iloc[0], df['Date'].iloc[-1]])


查看完整回答
反對 回復 2023-02-07
  • 1 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號