Matplotlib,一次设置多个参数?

2024-04-20 12:04:05 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有一种方法可以同时为多个图形设置参数-基本上定义一个默认值?你知道吗

我现在拥有的:

fig = plt.figure()
ax1 = fig.add_subplot(313)
ax1.scatter(entries,gvalues[0], color='b', marker = '1')
ax1.scatter(entries,gvalues[1], color = 'g', marker = 'v')
xticks([z for z in range(N)], namelist)
ylabel('Label1', ha='center', labelpad=28)
ylim(0,)
ax2 = fig.add_subplot(312, sharex=ax1)
ax2.scatter(entries,gvalues[2], color = 'b', marker = '1')
ax2.scatter(entries,gvalues[3], color= 'g', marker = 'v')
ylabel('Label2', ha='center', labelpad=28)
setp(ax2.get_xticklabels(),visible=False)
ylim(0,)

我将有一些这样的图,如果我不必每次都设置ha = 'center', labelpad = 28ylim(0,),那就太好了。你知道吗

干杯


Tags: addfigmarkercolorentriescenterhascatter
1条回答
网友
1楼 · 发布于 2024-04-20 12:04:05

您可以使用字典存储选项,如下所示:

font_options = dict(ha='center', labelpad=28)

ax1.set_ylabel('Label1', **font_options)

...

ax2.set_ylabel('Label2', **font_options)

**font_options参数将解压字典中的每个键值对,并在ylabel函数中应用它们。你知道吗

类似地,对于ylim选项,可以将限制存储在变量中,例如:

ylimit = 0

ylim(ylimit,)

相关问题 更多 >