[英]matplotlib axis label lost when increasing y tick labels for subplot
why are the y-axis labels lost when specifying: ax.set_yticklabels(ax.get_yticklabels(), fontsize=16)
为什么指定时y轴标签会丢失:
ax.set_yticklabels(ax.get_yticklabels(), fontsize=16)
%pylab inline
import pandas as pd
import seaborn as sns; sns.set()
df = pd.DataFrame({'dt':['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04'], 'category':['a', 'b', 'a', 'b'], 'foo':[10, 15, 8, 13], 'bar':[12, 8, 5, 18]})
df['dt'] = pd.to_datetime(df['dt'])
plt.figure()
ax0 = plt.subplot(211)
ax = sns.lineplot(x='dt', y='foo', data=df, hue='category', ax=ax0)
ax.set_yticklabels(ax.get_yticklabels(), fontsize=16)
plt.show()
plt.yticks(fontsize=16)
works just fine though. plt.yticks(fontsize=16)
工作得很好。 However, I must use theo object-oriented API as I use this in a more complex plot.但是,我必须使用面向对象的 API,因为我在更复杂的 plot 中使用它。 Otherwise,
plt
refers to the wrong axis.否则,
plt
指的是错误的轴。
Try to replace it with:尝试将其替换为:
ax.set_yticklabels(ax.get_yticks(), fontsize=16)
A short explaination why ax.set_yticklabels(ax.get_yticklabels(), fontsize=16)
gives you empty y labels:一个简短的解释为什么
ax.set_yticklabels(ax.get_yticklabels(), fontsize=16)
给你空 y 标签:
If you print (ax.get_yticklabels()[:])
, it gives you:如果你
print (ax.get_yticklabels()[:])
,它会给你:
[Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, '')]
Notice that the last item in every sublist is an empty string, they will be regarded as your ylabel.请注意,每个子列表中的最后一项是一个空字符串,它们将被视为您的 ylabel。 It is thus equivalent to
ax.set_yticklabels(labels=[], fontsize=16)
因此它等价于
ax.set_yticklabels(labels=[], fontsize=16)
You can just use tick_params
function and specify which axis.您可以只使用
tick_params
function 并指定哪个轴。 For both axis, use axis='both'
对于两个轴,使用
axis='both'
ax.tick_params(axis='y', labelsize=16)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.