在Python中同时出现两个数字

2024-06-12 17:26:54 发布

您现在位置: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()

错误数字: enter image description here

当它们都运行时,数字应该是这样的 enter image description here


Tags: orsourceforifsoplt数字temp
1条回答
网友
1楼 · 发布于 2024-06-12 17:26:54

如注释中所述,您没有指定axis object,因此plt.XYZ只接受最后一个对象来执行函数。解决此问题的一种方法是将每个axis对象存储在一个变量中,这样就可以分别处理它们。下面是一个尽可能接近您的代码的示例:

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,我们可以稍后存储和重用它。你知道吗

相关问题 更多 >