首页 > 解决方案 > 在 Python 中绘制具有三个变量的图形

问题描述

我试图在同一个图中显示三个变量的可视化。我会更好地解释,这是代码:

import pandas as pd
from matplotlib import pyplot as plt 

an_1 = pd.read_csv('an_1.csv', header=None, names=('Pd', 'V')) # M = 10 ^ 3 (gamma=0.01)

# ex for stack: an_1 = pd.DataFrame(data = {'Pd': [0.5,0.6,0.7,0.8], 'V':[200,210,230,240]})

plt.figure(figsize=(8,5), dpi=100)

plt.plot (an_1.Pd, an_1.V, 'r*--', label='Analyt_1')

perc_excedd = pd.read_csv('perc.csv', header=None, names=('Pd', 'V', 'exc'))

# ex for stack: perc_excedd = pd.DataFrame(data = {'Pd': [0.5,0.5,0.5,0.4,0.4,0.4], 
#'V':[200,210,220,200,210,220], 'perc':[0.1,0.1,0.2,0.3,0.1,0.2,0.3]})

基本上an1.csv有不同的值Pd和一个特定的值V。在perc.csv我有一个单一的值Pd,其中的不同值perc_exceed对应于不同的值V。在评论中,我只是放了随机值来帮助说明清楚。

我想拥有我已经拥有的图表,并在其中添加另一个 y 轴,其点perc_exceed取决于Pd和 on V

希望我已经足够清楚了。谢谢!

标签: pythonplot

解决方案


您可以使用该twiny功能。

ax1 = plt.gca() # get the current axis
ax2 = ax1.twinx() # get another y axis.
ax1 .plot (an_1.Pd, an_1.V, 'r*--', label='Analyt_1')
ax2 .plot (perc_excedd .Pd, perc_excedd .V, 'g*--', label='Excedd')

推荐阅读