![](/img/trans.png)
[英]What is the difference between drawing plots using plot, axes or figure in matplotlib?
[英]Drawing filled shapes between different axes in matplotlib
我想用填充的三角形表示我在主子圖中的某些數據點與其他子圖之間的關系。 我發現Connection Patch允許您在不同的軸之間繪制線/箭頭,但不能填充形狀。 由於我想要一個實心三角形,因此我嘗試提取面片的坐標(在主子圖的軸坐標中)並繪制具有相同坐標的多邊形。 然后,多邊形被子圖的軸切掉。 如何使它們在情節之間也可見?
這是當前的樣子 。
這是一個最小的工作示例:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gs
import numpy as np
fig, ax = plt.subplots()
ax.axis('off')
grid_t = gs.GridSpec(4,3)
ax0a = fig.add_subplot(grid_t[0:1,0:1])
ax0b = fig.add_subplot(grid_t[0:1,1:2])
ax0c = fig.add_subplot(grid_t[0:1,2:3])
ax1 = fig.add_subplot(grid_t[1:4,:])
xl = ax0a.get_xlim()
yl = ax0a.get_ylim()
ptAl = (xl[0], yl[0])
ptAr = (xl[1], yl[0])
ptD1 = (0,0)
ptD2 = (1,1)
ptD3 = (2,1)
ax1.plot([-1,0,1,2,3],[2,0,1,1,-1],'ko')
from matplotlib.patches import ConnectionPatch
for pts,axs,num in [[ptD1,ax0a,1],[ptD2,ax0b,2],[ptD3,ax0c,3]]:
con1 = ConnectionPatch(xyA=pts, xyB=ptAl, coordsA="data", coordsB="data",
axesA=ax1, axesB=axs,color='grey',shrinkA=0,shrinkB=0)
ax1.add_artist(con1)
con2 = ConnectionPatch(xyA=pts, xyB=ptAr, coordsA="data", coordsB="data",
axesA=ax1, axesB=axs,color='grey',shrinkA=0,shrinkB=0)
ax1.add_artist(con2)
line2=con2.get_path().vertices
line1=con1.get_path().vertices
zoomcoords = sorted(np.concatenate((line1[1:],line2)),key=lambda x: x[0])
triangle = plt.Polygon(zoomcoords,ec='black',fc='red',zorder=100)
ax1.add_artist(triangle)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.