Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 旋转后没有标记标签_Python 2.7_Matplotlib - Fatal编程技术网

Python 2.7 旋转后没有标记标签

Python 2.7 旋转后没有标记标签,python-2.7,matplotlib,Python 2.7,Matplotlib,当我要在绘图上旋转记号标签时,记号标签将消失。 代码如下: from matplotlib import pyplot as plt fg = plt.figure() fg.canvas.set_window_title('My plot') ax = fg.add_subplot(1,1,1) ax.plot(my_points) ax.set_xticklabels(ax.xaxis.get_majorticklabels(), rotation=90) 在绘制图形之前,标签为

当我要在绘图上旋转记号标签时,记号标签将消失。 代码如下:

from matplotlib import pyplot as plt

fg = plt.figure()
fg.canvas.set_window_title('My plot')

ax = fg.add_subplot(1,1,1)   
ax.plot(my_points)
ax.set_xticklabels(ax.xaxis.get_majorticklabels(), rotation=90)

在绘制图形之前,标签为空。这么叫
ax.xaxis.get_majorticklabels()
before
plt.show()

解决这个问题的一种方法是调用
plt.xticks(rotation=90)
而不是

ax.set_xticklabels(ax.xaxis.get_majorticklabels(), rotation='vertical')
解决此问题的另一种方法是在调用
ax.xaxis.get_majorticklabels()
之前调用
fg.canvas.draw()
,以使用非空字符串填充
Text
标签,但这是低效的,因为使用
fg.canvas.draw()
plt.show()
渲染图形两次,这并不是真的必要,因为您可以只使用
plt.xticks


import numpy as np
from matplotlib import pyplot as plt

fg = plt.figure()
fg.canvas.set_window_title('My plot')
my_points = np.random.randint(10, size=10)
ax = fg.add_subplot(1,1,1)   
ax.plot(my_points)
plt.xticks(rotation=90)
plt.show()