在matplotlib中旋转次要刻度

时间:2016-12-30 04:08:49

标签: python matplotlib

我正在绘制以下图表:

enter image description here

使用以下代码:

fig, ax = plt.subplots(figsize=(20, 3))
mpf.candlestick_ohlc(ax,quotes, width=0.01)
ax.xaxis_date()
ax.xaxis.set_minor_locator(mpl.dates.HourLocator(interval=4) )
ax.xaxis.set_minor_formatter(mpl.dates.DateFormatter('%H:%M')) 
plt.xticks(rotation = 90)

plt.grid(True)
plt.show()

我还要调整次要标记:我该怎么做?

辅助问题是否可以通过一个命令旋转主要和次要刻度?

3 个答案:

答案 0 :(得分:7)

您可以按一行plt.setp(ax.xaxis.get_minorticklabels(), rotation=90)的代码进行轮播。

答案 1 :(得分:2)

当我自己处理问题时,我发现您也可以使用tick_params通过一条语句轻松完成此操作:

ax.tick_params(axis="x", which="both", rotation=45)

这将旋转您的 x axis上的标签,而which选项可让您在次要,专业或两者之间进行选择。如果您有多个图,则必须对图中的每个图执行此操作。

答案 2 :(得分:1)

通过稍微探索,我发现ax.get_xminorticklabels()是一个带有text类元素的列表。

>>> print(type(ax.get_xminorticklabels()[0])) 
<class 'matplotlib.text.Text'>

text可以是rotated

>>> for text in ax.get_xminorticklabels():
>>>     text.set_rotation(90)

xminorticklabels_rotate_example

你必须要小心它们不重叠。