簡體   English   中英

在 matplotlib 中的子圖上繪制輪廓

[英]Overplotting contours over subplots in matplotlib

我有 2 組數據,我必須在條形子圖中顯示,如下所示: 條形圖

第二組只是我需要從中繪制輪廓的數據。 matplotlib 中是否有辦法在與條形子圖相同的圖形上繪制輪廓以獲得如下效果:

想要的效果

我用來生成子圖的代碼:

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

另外,我將非常感謝有關如何使這些子圖呈正方形的建議(如第二張圖片)

您可以簡單地使用幾何 (1,1,1) 創建一個新的子圖,也就是說,占據子圖的所有可用空間,從而覆蓋所有其他子圖。

如果您想查看下面的子圖(使用ax.patch.set_visible(False) ),您必須確保隱藏該頂軸的背景補丁。 或者,由於您要刪除軸上的所有刻度,您可以使用set_axis_off()刪除所有刺和裝飾,包括背景補丁。

至於方形子圖,如果你創建一個方形圖,你會得到方形子圖

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)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM