我想在python matplotlib中旋转图形的x标签以避免重叠,但是以下命令似乎不起作用(请参见图1 x轴的重叠部分)。我该怎么办?
plt.xticks(rotation=90)
总体效果如下所示: figure 1
我的所有代码如下所示:
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist
import pandas as pd
# read data
# data = pd.read_csv('D:\my_program\ex\data.csv',names=('x','y'))
data = pd.DataFrame({'x':[-3.06E-01,3.02E+00,6.69E+00,8.71E+00,9.06E+00,8.76E+00,9.78E+00,1.15E+01,1.25E+01,1.35E+01,1.58E+01,1.68E+01,1.88E+01,1.98E+01,2.08E+01,2.18E+01,2.35E+01,2.58E+01,2.84E+01,3.11E+01,3.28E+01,3.44E+01,3.58E+01,3.68E+01,3.78E+01,4.25E+01,4.67E+01,4.90E+01,5.07E+01,5.37E+01,5.60E+01,5.97E+01,6.80E+01,7.03E+01,7.13E+01,7.30E+01,7.36E+01,7.46E+01,7.53E+01,7.67E+01,7.93E+01,8.20E+01,8.30E+01,8.36E+01,8.43E+01,8.50E+01,8.60E+01,8.70E+01,8.87E+01,8.97E+01,9.17E+01,9.30E+01,9.40E+01,9.57E+01,1.00E+02,1.03E+02,1.05E+02,1.08E+02,1.16E+02,1.18E+02],'y':[8.00E+00,9.10E+00,1.35E+01,2.08E+01,2.73E+01,3.71E+01,4.51E+01,4.95E+01,5.06E+01,4.99E+01,4.55E+01,4.62E+01,4.77E+01,5.03E+01,5.35E+01,5.79E+01,6.01E+01,6.05E+01,6.23E+01,6.52E+01,6.74E+01,6.78E+01,7.07E+01,7.32E+01,7.69E+01,8.74E+01,5.22E+01,5.15E+01,5.25E+01,5.40E+01,5.58E+01,5.62E+01,5.66E+01,5.81E+01,5.95E+01,6.06E+01,6.32E+01,6.68E+01,7.01E+01,7.41E+01,7.27E+01,7.27E+01,7.45E+01,7.70E+01,8.14E+01,8.43E+01,8.87E+01,9.16E+01,9.23E+01,9.16E+01,9.12E+01,9.23E+01,9.42E+01,9.60E+01,6.76E+01,6.91E+01,6.95E+01,6.98E+01,6.99E+01,7.13E+01]})
x = data.x
y = data.y
x[0] = 0
fig = plt.figure()
ax = axisartist.Subplot(fig, 111)
fig.add_axes(ax)
ax.axis["bottom"].set_axisline_style("-|>", size = 1.5)
ax.axis["left"].set_axisline_style("-|>", size = 1.5)
ax.axis["top"].set_visible(False)
ax.axis["right"].set_visible(False)
plt.plot(x,y)
# plot beta_1
plt.scatter(data.x[25],data.y[25],color='gray')
plt.plot((0,data.x[25]),(data.y[25],data.y[25]),'--',color='gray')
plt.plot((data.x[25],data.x[25]),(0,data.y[25]),'--',color='gray')
plt.text(data.x[25]+5,data.y[25],r'$\beta_1$')
# plot second point
plt.scatter(data.x[26],data.y[26],color='gray')
plt.plot((0,data.x[26]),(data.y[26],data.y[26]),'--',color='gray')
plt.plot((data.x[26],data.x[26]),(0,data.y[26]),'--',color='gray')
# plot beta_2
plt.scatter(data.x[53],data.y[53],color='gray')
plt.plot((0,data.x[53]),(data.y[53],data.y[53]),'--',color='gray')
plt.plot((data.x[53],data.x[53]),(0,data.y[53]),'--',color='gray')
plt.text(data.x[53]+5,data.y[53],r'$\beta_2$')
# set axis
plt.xlim(0,120)
plt.ylim(0,100)
plt.yticks([14,16,18,20],['','','','10%'])
plt.xticks([0,100,data.x[25],data.x[26]],[r'$t_0$','t1',r'$t_1$',r'$t_1+\Delta t_1$'])
plt.xticks(rotation=90)
plt.savefig('figure.jpg')
plt.show()