[英]Adjusting graphs with Matplotlib
I'm having some problems adjusting the font size of the numerical labels on y axis of my graphs. 我在调整图表y轴上数字标签的字体大小时遇到一些问题。 Adjusting the font size only seems to adjust the text in the legend box.
调整字体大小似乎只能调整图例框中的文本。
Adjusting the 'axes' doesn't work because I've used axes.ravel()
to help give a 2x2 set of four subplots. 调整“轴”不起作用,因为我已经使用
axes.ravel()
帮助提供了一个2x2的四个子axes.ravel()
。
"axes.set_xlabel(fontsize='large', fontweight='bold') AttributeError: 'numpy.ndarray' object has no attribute 'set_xlabel'"
“” 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
itself is an array of axes. axes
本身是axes
的数组。 So you want to do: 所以你想做:
for each_subplot in range(0,4):
axes[each_subplot].set_xlabel(fontsize='large', fontweight='bold')
or simpler: 或更简单:
for ax in axes:
ax.set_xlabel(fontsize='large', fontweight='bold')
axes
is an ndarray now, so you need to extract the element from array and call set_xlabel()
method on it. axes
现在是一个ndarray,因此您需要从array中提取元素并在其上调用set_xlabel()
方法。 Try this. 尝试这个。
for each_subplot in range(0,4):
axes[each_subplot].set_xlabel(fontsize='large', fontweight='bold')
In such situations, I would personally recommend using enumerate
which provides you access not only to the individual axis objects but also to the index which can be used to modify labels, for instance. 在这种情况下,我个人建议使用
enumerate
,该enumerate
不仅使您可以访问各个轴对象,而且还可以访问例如可用于修改标签的索引。 To flatten the axes
, you can either use axes.ravel()
or axes.flatten()
. 要展平
axes
,可以使用axes.ravel()
或axes.flatten()
。 Also, you can directly use axes.flatten()
in the enumerate
as shown below. 另外,您可以直接在
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.