gpt4 book ai didi

python - Matplotlib - axvspan 与子图

转载 作者:行者123 更新时间:2023-11-30 22:58:33 24 4
gpt4 key购买 nike

我正在为海岸工程应用程序编写一个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]]

关于python - Matplotlib - axvspan 与子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36136314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com