汪汪一只猫
编辑:我忘了你想要一个作为酒吧。此外,如果您不想弄乱所有这些datetime东西,您可以将年份绘制为 x 轴上的整数(月份是 1/12 的分数)。但是我发现,datetime一旦将所有内容都作为时间对象,使用就非常聪明。我不太熟悉直接pandas从matplotlib. 不过,我无法完全复制您的数据:要遵循下面的示例,您必须将多索引转换为单个日期时间索引,我认为这不会太难。import datetime as dtimport pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport matplotlib.dates as mdates#making fake datadates1 = pd.date_range('12-01-2007','06-01-2010',periods=9)data1 = np.random.randint(0,3598215,9)df1 = pd.DataFrame(data1,index=dates1,columns=['Values'])dates2 = pd.date_range('01-01-2006',periods=4,freq='1Y') #i don't get why but this starts at the end of 2006, near 2007df2 = pd.DataFrame([69,3000,5,791],index=dates2,columns=['Values'])#plottingfig, ax = plt.subplots()ax.bar(df2.index,df2['Values'],width=dt.timedelta(days=200),color='red',label='df2')ax.set_yscale('log')ax.set_ylabel('DF2 values',color='red')ax2 = ax.twinx()ax2.plot(df1.index,df1['Values'],color='blue',label='df1')ax2.set_yscale('log',)ax2.set_ylabel('DF1 values',color='blue')years = mdates.YearLocator() #locate years for the ticksax.xaxis.set_major_locator(years) #format the ticks to just show yearsxfmt = mdates.DateFormatter('%Y')ax.xaxis.set_major_formatter(xfmt)ax.legend(loc=0)ax2.legend(loc=2)