簡體   English   中英

Matplotlib - axvspan vs subplots

[英]Matplotlib - axvspan vs subplots

我正在為一個沿海工程應用程序編寫一個pythonic腳本,除其他外,它應該輸出一個包含兩個子圖的圖形。

問題是我想使用plt.axvspan()來遮蔽兩個子圖的一部分,但由於某種原因它只遮蔽其中一個。

請在下面找到我設置圖表的代碼部分的摘錄以及它當前輸出的圖(代碼后鏈接)。

感謝您的幫助,對不起,如果這是一個菜鳥問題(但事實上我確實是Python中的新手...以及一般的編程)但我無法在其他任何地方找到答案。

隨意添加任何注釋代碼。

# PLOTTING
# now we generate a figure with the bathymetry vs required m50 and another figure with bathy vs Hs

#1. Generate plots

fig = plt.figure() # Generate Figure
ax = fig.add_subplot(211) # add the first plot to the figure.
depth = ax.plot(results[:,0],results[:,1]*-1,label="Depth [mDMD]") #plot the first set of data onto the first set of axis.
ax2 = ax.twinx() # generate a secondary vertical axis with the same horizontal axis as the first
m50 = ax2.plot(results[:,0],results[:,6],"r",label="M50 [kg]") # plot the second set of data onto the second vertical axis
ax3 = fig.add_subplot(212) # generate the second subplot
hs = ax3.plot(results[:,0],results[:,2],"g",label="Hs(m)")

#Now we want to find where breaking starts to occur so we shade it on the plot.
xBreakingDistance = results[numpy.argmax(breakingIndex),0]

# and now we plot a box from the origin to the depth of breaking.

plt.axvspan(0,xBreakingDistance,facecolor="b",alpha=0.1) # this box is called a span in matplotlib (also works for axhspan)

# and then we write BREAKING ZONE in the box we just created

yLimits = ax.get_ylim() # first we get the range of y being plotted
yMiddle = (float(yLimits[1])-float(yLimits[0])) / 2 + yLimits[0] # then we calculate the middle value in y (to center the text)
xMiddle = xBreakingDistance / 2 # and then the middle value in x (to center the text)
#now we write BREAKING ZONE in the center of the box.
ax.text(xMiddle,yMiddle,"BREAKING ZONE",fontweight="bold",rotation=90,verticalalignment="center",horizontalalignment="center")

#FIGURE FORMATTING
ax.set_xlabel("Distance [m]") # define x label
ax.set_ylabel("Depth [mDMD]") # define y label on the first vertical axis (ax)
ax2.set_ylabel("M50 [kg]") # define y label on the second vertical axis (ax2)
ax.grid() # show grid
ax3.set_xlabel("Distance[m]") #define x label
ax3.set_ylabel("Hs[m]") # define y label
ax3.grid()
plt.tight_layout() # minimize subplot labels overlapping

# generating a label on a plot with 2 vertical axis is not very intuitive. Normally we would just write ax.label(loc=0)
combined_plots = depth+m50 #first we need to combine the plots in a vector
combined_labels = [i.get_label() for i in combined_plots] # and then we combine the labels
ax.legend(combined_plots,combined_labels,loc=0) # and finally we plot the combined_labels of the combined_plots
plt.savefig("Required M50(kg) along the trench.png",dpi=1000)
plt.close(fig)

輸出圖:

通過調用plt.axvspan ,您告訴matplotlib在當前活動的軸上創建axvspan (即在這種情況下,您創建的最后一個軸, ax3

你需要在你希望它出現的兩個軸上繪制axvspan 在這種情況下, axax3

所以,你可以這樣做:

ax.axvspan(0,xBreakingDistance,facecolor="b",alpha=0.1)    
ax3.axvspan(0,xBreakingDistance,facecolor="b",alpha=0.1)

或者在一行中:

[this_ax.axvspan(0,xBreakingDistance,facecolor="b",alpha=0.1) for this_ax in [ax,ax3]]

分析代碼並且無法重現代碼很困難。 我建議你建立一個最小的例子。 在任何情況下,請注意您正在調用“plt.axvspan(”這是對庫的一般調用。

你需要在“ax”和“ax2”(我認為)中明確說明你想要這個。

此外,如果您需要更多控制,請考慮使用補丁(我不知道axvspan):

import matplotlib.pyplot as plt
import matplotlib.patches as patches

fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.add_patch(
    patches.Rectangle(
        (0.1, 0.1),   # (x,y)
        0.5,          # width
        0.5,          # height
    )
)
fig1.savefig('rect1.png', dpi=90, bbox_inches='tight')

在示例中看到對“ax1”的調用? 只需制作類似於你的東西。 或者只是將axvspan添加到每個圖中。

暫無
暫無

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

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