在以下使用matplotlib的代码中,我还要旋转x轴的次刻度线标签。不幸的是
plt.setp(axes.xaxis.get_minorticklabels(), rotation=90)
也
for text in axes.get_xminorticklabels():
print("text rotated")
text.set_rotation(90)
有效果。如何在此设置中控制这些标签的方向?
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()
答案 0 :(得分:0)
您必须创建图,然后更新次要刻度标签的属性。
因此移动以下代码
for text in axes.get_xminorticklabels():
print("text rotated")
text.set_rotation(90)
情节创建代码下方
axes.semilogx(t, sinus)
这是您必须执行的操作的顺序: