为什么我在任何地方都没有提到的子图中有一个额外的图表
Why there is a extra graph in a subplot which i did not mention anywhere
我使用了以下代码,在最后一个图中你的子图为什么我有一个额外的图(红色曲线)
fig= plt.figure()
plt.rcParams.update({'font.size':24})
fig, (ax1,ax2,ax3)=plt.subplots(3,1,figsize=
(20,24),sharex='row',sharey='col')
p1=plt.plot(x_axis,Clean_Current_TEST,"g",x_axis,Clean_Current_Prediction1,"r--")
ax1.plot(x_axis,Clean_Current_TEST,"g",x_axis,Clean_Current_Prediction1,"r--", lw=3.0)
ax2.plot(x_axis,Clean_Current_TEST,"g",x_axis,Clean_Current_Prediction2,"r--", lw=3.0)
ax3.plot(x_axis,Clean_Current_TEST,"g",x_axis,Clean_Current_Prediction3,"b--", lw=3.0)#,x_axis,Clean_Current_Prediction3,"b--"
ax1.legend(p1[:2], ["Experimental Output","Predicted Output1"],loc='best');
ax2.legend(p1[:2], ["Experimental Output","Predicted Output2"],loc='best');
ax3.legend(p1[:2], ["Experimental Output","Predicted Output3"],loc='best');
ax1.title.set_text('Short Circuit Current of Clean Module vs
Time Graph')
fig.text(0.5, 0.01, 'Time(hr)', ha='left', va='center')
fig.text(0.01, 0.5, 'Clean Module Current(mA)', ha='center',
va='center', rotation='vertical')
[enter image description here][1]
plt.show()
plt.tight_layout(pad=1)
(子图的最后一个数字)
这里 minimum, reproducible example 重现了您遇到的问题:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1,1,20)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.arctan(x)
fig, (ax1,ax2) = plt.subplots(2,1)
plt.plot(x, y3, "g", x, y1, "r--")
ax1.plot(x, y3, "g", x, y1, "r--", lw=3.0)
ax2.plot(x, y3, "g", x, y2, "b--", lw=3.0)
plt.show()
似乎 plt.plot
函数实际上在 last 轴上绘制了它的图形。这就是为什么您在最后一个轴上看到 "red" 图,即使在轴 2 上绘制的线明确要求 "blue" 线。一般来说,如果你在不同的轴上绘制不同的东西,那么你不应该使用 matplotlib.pyplot
中的便捷方法,而应该使用 Axis
对象上的方法。
fig, (ax1,ax2) = plt.subplots(2,1)
ax1.plot(x, y3, 'g', x, y1, "r--", lw=3.0)
ax2.plot(x, y3, 'g', x, y2, "b--", lw=3.0)
plt.show()
我使用了以下代码,在最后一个图中你的子图为什么我有一个额外的图(红色曲线)
fig= plt.figure()
plt.rcParams.update({'font.size':24})
fig, (ax1,ax2,ax3)=plt.subplots(3,1,figsize=
(20,24),sharex='row',sharey='col')
p1=plt.plot(x_axis,Clean_Current_TEST,"g",x_axis,Clean_Current_Prediction1,"r--")
ax1.plot(x_axis,Clean_Current_TEST,"g",x_axis,Clean_Current_Prediction1,"r--", lw=3.0)
ax2.plot(x_axis,Clean_Current_TEST,"g",x_axis,Clean_Current_Prediction2,"r--", lw=3.0)
ax3.plot(x_axis,Clean_Current_TEST,"g",x_axis,Clean_Current_Prediction3,"b--", lw=3.0)#,x_axis,Clean_Current_Prediction3,"b--"
ax1.legend(p1[:2], ["Experimental Output","Predicted Output1"],loc='best');
ax2.legend(p1[:2], ["Experimental Output","Predicted Output2"],loc='best');
ax3.legend(p1[:2], ["Experimental Output","Predicted Output3"],loc='best');
ax1.title.set_text('Short Circuit Current of Clean Module vs
Time Graph')
fig.text(0.5, 0.01, 'Time(hr)', ha='left', va='center')
fig.text(0.01, 0.5, 'Clean Module Current(mA)', ha='center',
va='center', rotation='vertical')
[enter image description here][1]
plt.show()
plt.tight_layout(pad=1)
(子图的最后一个数字)
这里 minimum, reproducible example 重现了您遇到的问题:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1,1,20)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.arctan(x)
fig, (ax1,ax2) = plt.subplots(2,1)
plt.plot(x, y3, "g", x, y1, "r--")
ax1.plot(x, y3, "g", x, y1, "r--", lw=3.0)
ax2.plot(x, y3, "g", x, y2, "b--", lw=3.0)
plt.show()
似乎 plt.plot
函数实际上在 last 轴上绘制了它的图形。这就是为什么您在最后一个轴上看到 "red" 图,即使在轴 2 上绘制的线明确要求 "blue" 线。一般来说,如果你在不同的轴上绘制不同的东西,那么你不应该使用 matplotlib.pyplot
中的便捷方法,而应该使用 Axis
对象上的方法。
fig, (ax1,ax2) = plt.subplots(2,1)
ax1.plot(x, y3, 'g', x, y1, "r--", lw=3.0)
ax2.plot(x, y3, 'g', x, y2, "b--", lw=3.0)
plt.show()