繁体   English   中英

访问 matplotlib 中的子图

[英]accessing subplots in matplotlib

在我看来,matplotlib 中似乎有许多不同的轴和轴对象,我发现很难弄清楚是什么。 这里的代码正在做我想要的,但所有的情节都在第一个子情节中相互交叉。 我认为您可以使用索引访问 ax 以选择将每个图放入的子图,但这给了我:

'AxesSubplot' object is not subscriptable

这是代码,plot 包含 [x, y, 每个条的颜色]

def display(subplots):
fig = plt.figure(facecolor='black')
ax = fig.add_subplot(1, 3, 1)
ax.set_facecolor((0,0,0))
ax.spines['bottom'].set_color('white')
ax.spines['top'].set_color('white')
ax.spines['left'].set_color('white')
ax.spines['right'].set_color('white')
ax.xaxis.label.set_color('white')
ax.tick_params(axis='x', colors = 'white')
ax.tick_params(axis='y', colors = 'white')
for plot in subplots:
    plt.barh(list(plot[0]), plot[1], color = plot[2])

这是我所知道的能够更改显示特定部分的所有颜色但似乎无法访问子图的唯一方法。 有人可以解释一下这个例子中的 ax 和 fig 是什么,以及我如何能够将 plot 放入每个单独的子图中。

无花果,ax = subplots(n,n)

我可以使用 fig 来更改图形的属性,例如 fig.set_facecolor ax 是一个子图数组,我可以单独访问它们的属性。 固定代码,其中 subplots 变量是每个图的列表 [x, y, color list for bars, name] 和 ex 是每个 subplots 轴的列表,您可以在其中访问其属性。

    fig, ax = plt.subplots(3, 2)
fig.set_facecolor('black')
fig.tight_layout()
ax = ax.flatten()  #flatten to make iterable with subplots
for subax, plot in zip(ax, subplots):
    subax.set_facecolor((0,0,0))
    subax.spines['bottom'].set_color('white')
    subax.spines['top'].set_color('white')
    subax.spines['left'].set_color('white')
    subax.spines['right'].set_color('white')
    subax.xaxis.label.set_color('white')
    subax.tick_params(axis='x', colors = 'white')
    subax.tick_params(axis='y', colors = 'white')
    subax.barh(list(plot[0]), plot[1], color = plot[2])
    subax.set_title('Label {} by type'.format(plot[3]), color = 'white')
    subax.tick_params(labelsize = 5)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM