简体   繁体   中英

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. 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:

'AxesSubplot' object is not subscriptable

here's the code, plot contains[x, y, colour for each bar]

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.

fig, 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. 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.

    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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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