Matplotlib 使用twinx时旋转xtick标签时出现问题

Matplotlib 使用twinx时旋转xtick标签时出现问题,matplotlib,rotation,axis-labels,Matplotlib,Rotation,Axis Labels,我的X轴旋转有问题,我试着在没有错误的情况下旋转输出图,但没有结果 # Import Data #df = pd.read_csv("https://github.com/selva86/datasets/raw/master/economics.csv") x = total_test["Dia"].values[:]; y1 = total_test["Confirmados"].values[:]; y2 = total_test["Fallecidos"].values[:] # Pl

我的X轴旋转有问题,我试着在没有错误的情况下旋转输出图,但没有结果

# Import Data
#df = pd.read_csv("https://github.com/selva86/datasets/raw/master/economics.csv")
x = total_test["Dia"].values[:]; y1 = total_test["Confirmados"].values[:]; y2 = total_test["Fallecidos"].values[:]

# Plot Line1 (Left Y Axis)
fig, ax1 = plt.subplots(1,1,figsize=(10,8), dpi= 200)
ax1.plot(x, y1,'g^', color='tab:red')

# Plot Line2 (Right Y Axis)
ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis
ax2.plot(x, y2,'bs', color='tab:blue')

# Just Decorations!! -------------------
# ax1 (left y axis)
ax1.set_xlabel('Dias', fontsize=10)
ax1.set_ylabel('Personas Confirmadas', color='tab:red', fontsize=20)
ax1.tick_params(axis='y', rotation=0, labelcolor='tab:red' )

# ax2 (right Y axis)
ax2.set_ylabel("Personas Fallecidas", color='tab:blue', fontsize=20)
ax2.tick_params(axis='y', rotation=0, labelcolor='tab:blue')
ax2.set_title("Personas Confirmadas y Fallecidas por Covid-19 Peru", fontsize=15)
#ax2.set_xticks(x)
ax2.set_xticklabels(x[::],fontsize=10,rotation=90)
plt.show()

  • xaxis的任何命令都需要在
    ax2
    之前执行
  • 验证日期是否为
    datetime
    格式并设置为索引
将熊猫作为pd导入
将matplotlib.pyplot作为plt导入
#读取数据
df=pd.read_csv(“https://github.com/selva86/datasets/raw/master/economics.csv")
#验证date列是否为datetime格式并设置为索引
df['date']=pd.to_datetime(df['date'])
df.set_索引('date',inplace=True)
#密谋
#塑造形象
图,ax1=plt.子批次(1,1,figsize=(10,8))
#第一地块
ax1.绘图(df['pop'],color='tab:red')
#在创建ax2之前设置xticks旋转
plt.xticks(旋转=90)
#第二个绘图(右Y轴)
ax2=ax1.twinx()#在右侧创建“twin”轴
ax2.绘图(df['unemploy',color='tab:blue')
plt.show()

那就
plt.xticks(旋转=90)
怎么样呢?非常感谢“axaxis需要在ax2之前出现”的功能!