簡體   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