Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

python - Overplotting contours over subplots in matplotlib

I have 2 sets of data, one that I have to display in bar subplots like this: bar subplots

and second set is just data from which I need to draw contours. Is there a way in matplotlib to draw contours on the same figure as bar subplots over them to get effect like this:

desired effect

Code I use to generate subplots:

nl = 0
fig, axs = plt.subplots(16,16, sharex='col', sharey='row', gridspec_kw={'hspace': 0, 'wspace': 0}, dpi = 300)
for p in range(16):
    for r in range(16):
        axs[p,r].set_ylim(0,1.1)
        axs[p,r].set_xlim(-150,150)
        axs[p,r].xaxis.set_ticklabels([])
        axs[p,r].yaxis.set_ticklabels([])
        axs[p,r].xaxis.set_ticks([])
        axs[p,r].xaxis.set_ticks([])
        axs[p,r].tick_params(width=0)
        axs[p,r].bar(xp,wid[nl], width = 10)
        nl += 1

Also I would be really thankful for suggestions how to make those subplots square (like on the 2nd image)

question from:https://stackoverflow.com/questions/65600142/overplotting-contours-over-subplots-in-matplotlib

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can simply create a new subplot with geometry (1,1,1), that is to say, occupying all the available space for the subplots and therefore covering all the other subplots.

You'll have to be sure to hide the background patch of that top axes if you want to see the subplots below (using ax.patch.set_visible(False)). Or since you see to remove all ticks from the axes, you can use set_axis_off() which removes all spines and decorations, including the background patch.

As for square subplots, you get square subplots if you create a square figure

N = 5  # for demonstration
w = 4

fig, axs = plt.subplots(N,N, sharex='col', sharey='row', gridspec_kw={'hspace': 0, 'wspace': 0}, dpi=100, figsize=(w,w))
for ax in axs.flat:
    ax.set_ylim(0,1.1)
    ax.set_xlim(-150,150)
    ax.xaxis.set_ticklabels([])
    ax.yaxis.set_ticklabels([])
    ax.xaxis.set_ticks([])
    ax.xaxis.set_ticks([])
    ax.tick_params(width=0)
    #ax.bar(xp,wid[nl], width = 10)

# create another axes over the grid
ax0 = fig.add_subplot(111)
ax0.set_axis_off()  # hides all axes decoration (also hides the background)
#ax0.patch.set_visible(False)  # hides the background of the axes


# plot contour
# code from https://matplotlib.org/3.1.1/gallery/images_contours_and_fields/contour_demo.html#sphx-glr-gallery-images-contours-and-fields-contour-demo-py
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2

CS = ax0.contour(X, Y, Z)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...