繁体   English   中英

Matplotlib Plot 函数返回同一图中的线,

[英]Matplotlib Plot function returns lines in a same plot,

当前图仅生成一条线,但我希望同一图中的多条线遍历数据框df2变量

现在我被卡住了,因为函数的返回是数据框,我如何将它存储在'plot3'变量中?

如何在相同的图中绘制wp(i)tempp(i) {5 组日期} 等。

代表情节:

import pandas as pd
import random
def function(ConstantA, ConstantB, tst, temp, dtube):
    # some function
    randomlist = []
    for i in range(0, 5):
        n = random.randint(1, 30)
    wp= ConstantA * randomlist
    tempp=ConstantB * randomlist
    ycp = tst * randomlist
    yap = temp * randomlist
    pp = dtube * randomlist

    all = pd.DataFrame({'wp': wp, 'tempp': tempp, 'ycp': ycp, 'yap': yap, 'pp': pp, })


    return all

ConstantA = [0.01545, 0.6, 0.6, 0.6, 0.6];
ConstantB = [0.6885e-6, 0, 0, 0, 0];
tst = [523, 529, 531, 527, 533];
temp = [523, 524, 524, 524, 524];
dtube = [0.041, 0.041, 0.041, 0.041, 0.041, 0.041];

df2 = pd.DataFrame(list(zip(ConstantA, ConstantB, tst, temp, dtube)),
                   columns=['ConstantA', 'ConstantB', 'tst', 'temp', 'dtube'])

for index, row in df2.iterrows():
    plot3 = function(row['ConstantA'], row['ConstantB'], row['tst'], row['temp'], row['dtube'])

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
fig.suptitle('Cooled;Tst=523')
ax1.plot(wp, tempp)
ax1.set_ylabel('T(K)')
ax2.plot(wp, ycp)
ax2.set_ylabel('y(C%)')
ax3.plot(wp, yap)
ax3.set_ylabel('yA ()')
ax3.set_xlabel('w (1000 ka)')
ax4.plot(wp, pp)
ax4.set_ylabel('E (bar)')
ax4.set_xlabel('w (1000 kg)')

plt.tight_layout()  # Automatic adjustment of pyplot so ylabels dont overlap
plt.subplots_adjust(top=0.9)  # Adjust plots to put space beween title and subplot
plt.show()

我看到的问题很少。

您应该创建fig, ax1, ax2, ax3, ax4之前for -loop后来的情节plot3for -loop。 这样,您将绘制相同的图。

您应该使用numpy生成randomlist因为当randomlist列表是普通列表时,您不能执行ConstantA * randomlist

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# --- functions ---

def function(row):
    randomlist = np.random.randint(1, 30, size=5)

    return pd.DataFrame({
                'wp':    randomlist * row['ConstantA'],
                'tempp': randomlist * row['ConstantB'],
                'ycp':   randomlist * row['tst'],
                'yap':   randomlist * row['temp'],
                'pp':    randomlist * row['dtube']
            })
    
# --- main ---

data = {
    'ConstantA': [0.01545, 0.6, 0.6, 0.6, 0.6],
    'ConstantB': [0.6885e-6, 0, 0, 0, 0],
    'tst': [523, 529, 531, 527, 533],
    'temp': [523, 524, 524, 524, 524],
    'dtube': [0.041, 0.041, 0.041, 0.041, 0.041]
}    

df = pd.DataFrame(data)

# before `for`-loop

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
fig.suptitle('Cooled;Tst=523')
ax1.set_ylabel('T(K)')
ax2.set_ylabel('y(C%)')
ax3.set_ylabel('yA ()')
ax3.set_xlabel('w (1000 ka)')
ax4.set_ylabel('E (bar)')
ax4.set_xlabel('w (1000 kg)')

# `for`-loop

for index, row in df.iterrows():
    plot3 = function(row)
    
    ax1.plot(plot3['wp'], plot3['tempp'])
    ax2.plot(plot3['wp'], plot3['ycp'])
    ax3.plot(plot3['wp'], plot3['yap'])
    ax4.plot(plot3['wp'], plot3['pp'])

# after `for`-loop

plt.tight_layout()  # Automatic adjustment of pyplot so ylabels dont overlap
plt.subplots_adjust(top=0.9)  # Adjust plots to put space beween title and subplot
plt.show()

结果:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM