![](/img/trans.png)
[英]Combining a bar plot and a line plot in matplotlib without shifting the bar plot
[英]Trying to plot a line plot on a bar plot using Matplotlib
我正在尝试使用 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: global name 'subplots' is not defined
我没有您的数据,但使用 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()
作为基于此您需要的最小示例,
import numpy as np
import matplotlib.pyplot as plt
#Setup dummy data
N = 10
ind = np.arange(N)
bars = np.random.randn(N)
t = np.arange(0.01, 10.0, 0.01)
#Plot graph with 2 y axes
fig, ax1 = plt.subplots()
#Plot bars
ax1.bar(ind, bars, alpha=0.3)
ax1.set_xlabel('$x$')
# Make the y-axis label and tick labels match the line color.
ax1.set_ylabel('bar', color='b')
[tl.set_color('b') for tl in ax1.get_yticklabels()]
#Set up ax2 to be the second y axis with x shared
ax2 = ax1.twinx()
#Plot a line
ax2.plot(t, np.sin(0.25*np.pi*t), 'r-')
# Make the y-axis label and tick labels match the line color.
ax2.set_ylabel('sin', color='r')
[tl.set_color('r') for tl in ax2.get_yticklabels()]
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.