简体   繁体   English

访问 matplotlib 中的子图

[英]accessing subplots in matplotlib

it seems to me like theres many different axes and axis objects hanging around in matplotlib and im finding it hard to figure out whats what.在我看来,matplotlib 中似乎有许多不同的轴和轴对象,我发现很难弄清楚是什么。 this code here is doing what i want but all the plots are going over eachother in the first subplot.这里的代码正在做我想要的,但所有的情节都在第一个子情节中相互交叉。 i thought that you could access ax with indexes to pick which subplots to put each graph in but this gives me:我认为您可以使用索引访问 ax 以选择将每个图放入的子图,但这给了我:

'AxesSubplot' object is not subscriptable

here's the code, plot contains[x, y, colour for each bar]这是代码,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])

this is the only way I know of to be able to change all the colours of specific parts of the display but seems to be unable to access subplots.这是我所知道的能够更改显示特定部分的所有颜色但似乎无法访问子图的唯一方法。 can someone explain what ax and fig are in this example and how i would be able to plot into each individual subplot.有人可以解释一下这个例子中的 ax 和 fig 是什么,以及我如何能够将 plot 放入每个单独的子图中。

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

I can use fig to change attributes of the figure such as fig.set_facecolor ax is an array of subplots i can access individually for their attributes.我可以使用 fig 来更改图形的属性,例如 fig.set_facecolor ax 是一个子图数组,我可以单独访问它们的属性。 fixed code with subplots variable being a list of each plots [x, y, colour list for bars, name] and ex being a list of each subplots axis where you can access its attributes.固定代码,其中 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