gpt4 book ai didi

python - 尝试使用 Matplotlib 在条形图上绘制线图

转载 作者:行者123 更新时间:2023-11-28 20:41:45 26 4
gpt4 key购买 nike

我正在尝试使用 MatPlotLib 在条形图上绘制线图。到目前为止,我已经显示了两个图,但我无法获得具有不同比例的辅助 y 轴。当我尝试放入另一个时,它会覆盖原始比例。我的代码在下面

def hridef_base(x):
if x == 1:

deltatlapexit = ExitSummary['Avg tLap'] - ExitSummary2['Avg tLap']

plt.figure()
index = (EntrySummary.index + 1)
barwidth = 0.2

plt.ylim(0.0, 65.0)
plt.bar(index-barwidth, EntrySummary['Avg FRH'], barwidth, color='r', yerr=EntrySummary['FRH StDev'], ecolor='k', label='Entry')
plt.bar(index, ApexSummary['Avg FRH'], barwidth, color='#4169E1', yerr=ApexSummary['FRH StDev'], ecolor='k', label='Apex')
plt.bar(index+barwidth, ExitSummary['Avg FRH'], barwidth, color='#32CD32', yerr=ExitSummary['FRH StDev'], ecolor='k', label='Exit')

plt.plot(index, deltatlapexit, color='k', label='Entry')

plt.xlabel('Turn Number')
plt.ylabel('Average FRideH')
plt.title('Average FRideH for Baseline setup')
plt.xticks(index)
plt.legend()

else:
print "Baseline FRideH Not Selected"

欢迎就此事提出任何建议,但请告诉我该怎么做,而不是发布网站链接。我需要了解为什么我无法做到这一点。

提前致谢。如果我遗漏了什么,请发表评论。

更新

感谢下面的评论,图表现在有两个独立的 y 轴。条形数据的 y 标签如何跳到右边,如果我尝试绘制 y 标签线,我会收到属性错误。

def hridef_base(x):
if x == 1:

deltatlapexit = ExitSummary['Avg tLap'] - ExitSummary2['Avg tLap']

fig, axis1 = subplots()
index = (EntrySummary.index + 1)
barwidth = 0.2

axis1.ylim(0.0, 65.0)
axis1.bar(index-barwidth, EntrySummary['Avg FRH'], barwidth, color='r', yerr=EntrySummary['FRH StDev'], ecolor='k', label='Entry')
axis1.bar(index, ApexSummary['Avg FRH'], barwidth, color='#4169E1', yerr=ApexSummary['FRH StDev'], ecolor='k', label='Apex')
axis1.bar(index+barwidth, ExitSummary['Avg FRH'], barwidth, color='#32CD32', yerr=ExitSummary['FRH StDev'], ecolor='k', label='Exit')

axis2 = axis1.twinx()
axis2.set_ylim(-10.0, 10.0)
axis2.plot(index, deltatlapexit, color='k', label='tDelta')

axis1.xlabel('Turn Number')
axis1.ylabel('Average FRideH')
axis2.set_ylabel('tDelta')
axis1.title('Average FRideH for Baseline setup')
axis1.xticks(index)
axis1.legend()
axis2.legend()

else:
print "Baseline FRideH Not Selected"

我会插入一张图片,但我需要 10 个重复点...

我收到的错误是: NameError: 全局名称 'subplots' 未定义

最佳答案

我没有你的数据,但使用 barplot example来自 matplotlib,这里有一个带有条形图和线图的示例,带有独立缩放的 y 轴:

from matplotlib import pyplot as plt
import numpy as np

plt.figure()
N = 5
menMeans = (20, 35, 30, 35, 27)
menStd = (2, 3, 4, 1, 2)
width = 0.35 # the width of the bars
womenMeans = (25, 32, 34, 20, 25)
womenStd = (3, 5, 2, 3, 3)
ind = np.arange(N)
plt.ylim(0.0, 65.0)
plt.bar(ind, menMeans, width, color='r', yerr=menStd, label='Men means')
plt.bar(ind+width, womenMeans, width, color='y', yerr=womenStd, label='Women means')
plt.ylabel('Bar plot')

x = np.linspace(0, N)
y = np.sin(x)
axes2 = plt.twinx()
axes2.plot(x, y, color='k', label='Sine')
axes2.set_ylim(-1, 1)
axes2.set_ylabel('Line plot')

plt.show()

enter image description here

关于python - 尝试使用 Matplotlib 在条形图上绘制线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32474434/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com