Python 同时出现两个数字
Two figures simultaneously in Python
我有一个代码可以单独或同时运行两个图形。问题是当它们同时 运行 时。它们都没有正确显示。我尝试使用 plt.subplotlib
但它没有帮助。
这是代码
import numpy as np
import matplotlib.pyplot as plt
Choice = input('Which figure do you want to run: \n *for Fig.1 , write 1 \n *for Fig.2, write 2 \n *for both , write b \n--> ')
Etta_Source={};Etta_Source1={};QKN_Source={}
for E_So in [100,200,400,800,600,1000]:
for Temp in range(0,110,10):
Etta = 0.8 - ((3.41*Temp)+(0.014*(Temp)**2))/E_So
if Choice == '1' or Choice == 'b':
QKN_Source[Temp] = Etta*2.3*E_So
Etta_Source[Temp] = Etta
if E_So == 1000 and (Choice == '2' or Choice == 'b'):
Etta_Source1[Temp]=Etta
if Choice == '1' or Choice == 'b':
plt.figure(1)
plt.plot(QKN_Source.keys(),QKN_Source.values(),label='D = %s'%E_So)
QKN_Source.clear()
if Choice == '2' or Choice == 'b':
plt.figure(2)
plt.plot(Etta_Source.keys(),Etta_Source.values(),label='C= %s'%E_So)
Etta_Source.clear()
if Choice == '2' or Choice == 'b':
plt.axhline(0.8,ls='-.',color='skyblue')
plt.fill_between(Etta_Source1.keys(),Etta_Source1.values(),0.8,facecolor='wheat')
plt.axhspan(0.8,1.0,alpha=0.5, color='c')
plt.axis([0,100,0,1])
plt.text(50, 0.9, r'Area 1',fontsize=14,fontweight='bold')
plt.text(80, 0.6, r'Area 2',fontsize=14,fontweight='bold')
plt.yticks(np.arange(0,1.1,0.1))
plt.ylabel('W',fontsize=14)
if Choice == '1' or Choice == 'b':
plt.axis([0,100,0,2000])
plt.ylabel('V',fontsize=14)
plt.grid(True)
plt.title('Results',color='black',fontsize=15,fontweight='bold')
plt.xticks(range(0,110,10),fontsize=14)
plt.xlabel(r'$\beta_a + \alpha_a$',fontsize=14)
plt.tight_layout()
plt.legend(loc=1)
plt.show()
错误的数字:
当它们都是 运行 时,数字应该是这样的
如评论中所述,您未指定 axis object,因此 plt.XYZ
仅使用最后一个对象来执行该功能。解决此问题的一种方法是将每个轴对象存储在一个变量中,以便您可以分别处理它们。这是一个尽可能接近您的代码的示例:
import numpy as np
import matplotlib.pyplot as plt
Choice = input('Which figure do you want to run: \n *for Fig.1 , write 1 \n *for Fig.2, write 2 \n *for both , write b \n--> ')
Etta_Source={};Etta_Source1={};QKN_Source={}
for E_So in [100,200,400,800,600,1000]:
for Temp in range(0,110,10):
Etta = 0.8 - ((3.41*Temp)+(0.014*(Temp)**2))/E_So
if Choice == '1' or Choice == 'b':
QKN_Source[Temp] = Etta*2.3*E_So
Etta_Source[Temp] = Etta
if E_So == 1000 and (Choice == '2' or Choice == 'b'):
Etta_Source1[Temp]=Etta
if Choice == '1' or Choice == 'b':
fig1 = plt.figure(1)
#get axis object for this figure
ax1 = plt.gca()
#now perform all commands with this axis object
ax1.plot(QKN_Source.keys(),QKN_Source.values(),label='D = %s'%E_So)
QKN_Source.clear()
if Choice == '2' or Choice == 'b':
fig2 = plt.figure(2)
#and create a separate axis object for the second figure
ax2 = plt.gca()
ax2.plot(Etta_Source.keys(),Etta_Source.values(),label='C= %s'%E_So)
Etta_Source.clear()
axlist = [] #collect the axes for the choices
if Choice == '2' or Choice == 'b':
ax2.axhline(0.8,ls='-.',color='skyblue')
ax2.fill_between(Etta_Source1.keys(),Etta_Source1.values(),0.8,facecolor='wheat')
ax2.axhspan(0.8,1.0,alpha=0.5, color='c')
ax2.axis([0,100,0,1])
ax2.text(50, 0.9, r'Area 1',fontsize=14,fontweight='bold')
ax2.text(80, 0.6, r'Area 2',fontsize=14,fontweight='bold')
ax2.set_yticks(np.arange(0,1.1,0.1))
ax2.set_ylabel('W',fontsize=14)
axlist.append(ax2)
if Choice == '1' or Choice == 'b':
ax1.axis([0,100,0,2000])
ax1.set_ylabel('V',fontsize=14)
axlist.append(ax1)
#now we have to apply these commands to both axis objects, i.e., both figures
for ax in axlist:
ax.grid(True)
ax.set_title('Results',color='black',fontsize=15,fontweight='bold')
ax.set_xticks(range(0,110,10))
ax.set_xlabel(r'$\beta_a + \alpha_a$',fontsize=14)
ax.legend(loc=1)
plt.show()
并且由于您提到您尝试使用子图 - they return an axis object,我们可以存储并在以后重新使用。
我有一个代码可以单独或同时运行两个图形。问题是当它们同时 运行 时。它们都没有正确显示。我尝试使用 plt.subplotlib
但它没有帮助。
这是代码
import numpy as np
import matplotlib.pyplot as plt
Choice = input('Which figure do you want to run: \n *for Fig.1 , write 1 \n *for Fig.2, write 2 \n *for both , write b \n--> ')
Etta_Source={};Etta_Source1={};QKN_Source={}
for E_So in [100,200,400,800,600,1000]:
for Temp in range(0,110,10):
Etta = 0.8 - ((3.41*Temp)+(0.014*(Temp)**2))/E_So
if Choice == '1' or Choice == 'b':
QKN_Source[Temp] = Etta*2.3*E_So
Etta_Source[Temp] = Etta
if E_So == 1000 and (Choice == '2' or Choice == 'b'):
Etta_Source1[Temp]=Etta
if Choice == '1' or Choice == 'b':
plt.figure(1)
plt.plot(QKN_Source.keys(),QKN_Source.values(),label='D = %s'%E_So)
QKN_Source.clear()
if Choice == '2' or Choice == 'b':
plt.figure(2)
plt.plot(Etta_Source.keys(),Etta_Source.values(),label='C= %s'%E_So)
Etta_Source.clear()
if Choice == '2' or Choice == 'b':
plt.axhline(0.8,ls='-.',color='skyblue')
plt.fill_between(Etta_Source1.keys(),Etta_Source1.values(),0.8,facecolor='wheat')
plt.axhspan(0.8,1.0,alpha=0.5, color='c')
plt.axis([0,100,0,1])
plt.text(50, 0.9, r'Area 1',fontsize=14,fontweight='bold')
plt.text(80, 0.6, r'Area 2',fontsize=14,fontweight='bold')
plt.yticks(np.arange(0,1.1,0.1))
plt.ylabel('W',fontsize=14)
if Choice == '1' or Choice == 'b':
plt.axis([0,100,0,2000])
plt.ylabel('V',fontsize=14)
plt.grid(True)
plt.title('Results',color='black',fontsize=15,fontweight='bold')
plt.xticks(range(0,110,10),fontsize=14)
plt.xlabel(r'$\beta_a + \alpha_a$',fontsize=14)
plt.tight_layout()
plt.legend(loc=1)
plt.show()
错误的数字:
当它们都是 运行 时,数字应该是这样的
如评论中所述,您未指定 axis object,因此 plt.XYZ
仅使用最后一个对象来执行该功能。解决此问题的一种方法是将每个轴对象存储在一个变量中,以便您可以分别处理它们。这是一个尽可能接近您的代码的示例:
import numpy as np
import matplotlib.pyplot as plt
Choice = input('Which figure do you want to run: \n *for Fig.1 , write 1 \n *for Fig.2, write 2 \n *for both , write b \n--> ')
Etta_Source={};Etta_Source1={};QKN_Source={}
for E_So in [100,200,400,800,600,1000]:
for Temp in range(0,110,10):
Etta = 0.8 - ((3.41*Temp)+(0.014*(Temp)**2))/E_So
if Choice == '1' or Choice == 'b':
QKN_Source[Temp] = Etta*2.3*E_So
Etta_Source[Temp] = Etta
if E_So == 1000 and (Choice == '2' or Choice == 'b'):
Etta_Source1[Temp]=Etta
if Choice == '1' or Choice == 'b':
fig1 = plt.figure(1)
#get axis object for this figure
ax1 = plt.gca()
#now perform all commands with this axis object
ax1.plot(QKN_Source.keys(),QKN_Source.values(),label='D = %s'%E_So)
QKN_Source.clear()
if Choice == '2' or Choice == 'b':
fig2 = plt.figure(2)
#and create a separate axis object for the second figure
ax2 = plt.gca()
ax2.plot(Etta_Source.keys(),Etta_Source.values(),label='C= %s'%E_So)
Etta_Source.clear()
axlist = [] #collect the axes for the choices
if Choice == '2' or Choice == 'b':
ax2.axhline(0.8,ls='-.',color='skyblue')
ax2.fill_between(Etta_Source1.keys(),Etta_Source1.values(),0.8,facecolor='wheat')
ax2.axhspan(0.8,1.0,alpha=0.5, color='c')
ax2.axis([0,100,0,1])
ax2.text(50, 0.9, r'Area 1',fontsize=14,fontweight='bold')
ax2.text(80, 0.6, r'Area 2',fontsize=14,fontweight='bold')
ax2.set_yticks(np.arange(0,1.1,0.1))
ax2.set_ylabel('W',fontsize=14)
axlist.append(ax2)
if Choice == '1' or Choice == 'b':
ax1.axis([0,100,0,2000])
ax1.set_ylabel('V',fontsize=14)
axlist.append(ax1)
#now we have to apply these commands to both axis objects, i.e., both figures
for ax in axlist:
ax.grid(True)
ax.set_title('Results',color='black',fontsize=15,fontweight='bold')
ax.set_xticks(range(0,110,10))
ax.set_xlabel(r'$\beta_a + \alpha_a$',fontsize=14)
ax.legend(loc=1)
plt.show()
并且由于您提到您尝试使用子图 - they return an axis object,我们可以存储并在以后重新使用。