[英]Adjusting graphs with Matplotlib
我在调整图表y轴上数字标签的字体大小时遇到一些问题。 调整字体大小似乎只能调整图例框中的文本。
调整“轴”不起作用,因为我已经使用axes.ravel()
帮助提供了一个2x2的四个子axes.ravel()
。
“” axes.set_xlabel(fontsize ='large',fontweight ='bold')AttributeError:'numpy.ndarray'对象没有属性'set_xlabel'”
#The part of the code that creates the subplots.
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(40,20), squeeze=False, sharey=True)
axes = axes.ravel()
font = FontProperties()
font = {'weight' : 'bold','size' : 22}
plt.rc('font', **font)
#Then under here are the loops that create each subplot.
for each_subplot in range(0,4):
axes.set_xlabel(fontsize='large', fontweight='bold')
#Selecting the input data goes here, but I left it out.
axes
本身是axes
的数组。 所以你想做:
for each_subplot in range(0,4):
axes[each_subplot].set_xlabel(fontsize='large', fontweight='bold')
或更简单:
for ax in axes:
ax.set_xlabel(fontsize='large', fontweight='bold')
axes
现在是一个ndarray,因此您需要从array中提取元素并在其上调用set_xlabel()
方法。 尝试这个。
for each_subplot in range(0,4):
axes[each_subplot].set_xlabel(fontsize='large', fontweight='bold')
在这种情况下,我个人建议使用enumerate
,该enumerate
不仅使您可以访问各个轴对象,而且还可以访问例如可用于修改标签的索引。 要展平axes
,可以使用axes.ravel()
或axes.flatten()
。 另外,您可以直接在enumerate
使用axes.flatten()
,如下所示。
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(8,5), squeeze=False, sharey=True)
for index, ax in enumerate(axes.ravel()):
ax.set_xlabel('X-label %s' %index, fontsize='large', fontweight='bold')
plt.tight_layout()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.