如何在Semilogx图中旋转次刻度的轴标签? [英] How to rotate axis labels for minor ticks within a semilogx plot?

查看:67
本文介绍了如何在Semilogx图中旋转次刻度的轴标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下使用 matplotlib 的代码中,我还想旋转 x 轴的小刻度标签.可惜都没有

Within the following code using matplotlib, I would like to rotate also the minor tick labels of the x axis. Unfortunately neither

plt.setp(axes.xaxis.get_minorticklabels(), rotation=90)

也没有

for text in axes.get_xminorticklabels():
    print("text rotated")
    text.set_rotation(90)

有效果.如何在此设置中控制这些标签的方向?

have an effect. How can I control in this setup the orientation of these labels?

import matplotlib
matplotlib.use('TkAgg')

import matplotlib.pyplot as plt

from matplotlib.ticker import FuncFormatter

fig, axes = plt.subplots()

import numpy as np

ONE_YEAR_IN_DAYS = 365

ONE_DAY_IN_TIMESTAMP_UNITS = 86400000000000

import pandas as pd

start = pd.Timestamp('2016-07-01')
end = pd.Timestamp('2017-07-02')

t = np.linspace(start.value, end.value, 100 * ONE_YEAR_IN_DAYS)

sinus = np.sin(2 * np.pi * 1 * t / ONE_DAY_IN_TIMESTAMP_UNITS)

t = end.value - t
t = 1000 * t / ONE_DAY_IN_TIMESTAMP_UNITS

plt.xticks(rotation=90)

plt.setp(axes.xaxis.get_minorticklabels(), rotation=90)

for text in axes.get_xminorticklabels():
    print("text rotated")
    text.set_rotation(90)

axes.semilogx(t, sinus)

def method_name():
    # return lambda y, _: '{:.16g}'.format(2*y)
    return lambda y, _: '{:.16g}'.format(2*y)


for axis in [axes.xaxis, axes.yaxis]:
    formatter = FuncFormatter(method_name())
    axis.set_major_formatter(formatter)
    axis.set_minor_formatter(formatter)

plt.show()

推荐答案

您必须创建绘图,然后更新次刻度标签的属性.

You have to create the plot then update the properties of the minor tick labels.

因此,移动以下代码

for text in axes.get_xminorticklabels():
    print("text rotated")
    text.set_rotation(90)

情节创建代码下方

axes.semilogx(t, sinus)

这是您必须执行的操作的顺序:

This is the order you must do things in:

  • 创建数据
  • 创建情节
  • 修改绘图属性

这篇关于如何在Semilogx图中旋转次刻度的轴标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆