【问题标题】:Rotate minor ticks in matplotlib在 matplotlib 中旋转次要刻度
【发布时间】:2016-12-30 04:08:49
【问题描述】:

我正在绘制以下图表:

使用以下代码:

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()

我还想旋转次要刻度:我该怎么做?

辅助问题有没有办法用一个命令同时旋转主要和次要刻度?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您可以按一行代码plt.setp(ax.xaxis.get_minorticklabels(), rotation=90)旋转。

    【讨论】:

      【解决方案2】:

      在自己处理问题时,我发现您也可以使用 tick_params 的单个语句轻松完成此操作:

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

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

      【讨论】:

        【解决方案3】:

        通过一点探索,我发现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)
        

        您只需要注意它们不要重叠。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-06-03
          • 1970-01-01
          • 2012-07-01
          • 1970-01-01
          • 2015-09-20
          • 2020-01-15
          • 1970-01-01
          相关资源
          最近更新 更多