[英]Python making combined bar and line plot with secondary y-axis
I am trying to plot some csv data. 我正在尝试绘制一些csv数据。 I would like to plot some csv data. 我想绘制一些csv数据。 The data is shown below. 数据如下所示。 I'm trying to plot columns 1-11 as a bar plot and column 12 as a line. 我正在尝试将1-11列绘制为条形图并将12列绘制为一条线。 I can make both plots separately using the following code, but how can I combine the plots and also have a secondary y-axis? 我可以使用以下代码分别绘制两个图,但是如何合并这些图并且还具有辅助y轴?
Here is a sample of the data I am plotting 这是我正在绘制的数据的示例
DateTime e1 e2 e3 e4 e5 e6 e7 e8 e9 e10 e11 p12
11/1/2014 1772 1926 1852 1513 1713 1568 1721 1822 1665 1449 1874 347
11/2/2014 19884 20365 19799 18017 18394 19383 20089 19929 20277 19522 19882 3710
11/3/2014 28697 29692 28881 25031 26731 28207 29095 29109 29577 28714 28926 5614
11/4/2014 24906 26061 25174 21745 23623 24126 24954 25344 25679 24406 25288 4990
11/5/2014 9059 9821 9116 7546 8742 8530 8910 9372 9214 8227 9366 1734
11/6/2014 1396 1691 1569 1176 1353 1223 1347 1541 1355 1044 1580 282
11/7/2014 10039 10416 9902 8223 9667 9511 9877 10106 10180 9524 10138 1857
11/8/2014 26746 27694 27128 23694 25520 26351 27176 27155 27704 26979 26995 5155
11/9/2014 14797 15567 14818 13556 14499 14244 14899 14979 15225 14171 14929 2846
11/10/2014 26059 27443 26573 22844 24655 25538 26658 26690 27303 26094 26471 5304
Here is the code I am using to plot them separately 这是我用来分别绘制它们的代码
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("data.csv", index_col="DateTime", parse_dates=True)
df.iloc[:,[0,1,2,3,4,5,6,7,8,9,10]].plot(kind='bar')
df.iloc[:,11].plot(linestyle='-', marker='o')
plt.show()
Unfortunately it seems impossible to plot a bar plot and a lineplot to the same axes in pandas if the x axis is a dates axis . 不幸的是, 如果x轴是日期轴,则似乎不可能在熊猫的相同轴上绘制条形图和线图 。
A workaround is to use a matplotlib barplot instead 解决方法是改用matplotlib barplot
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("data.csv", index_col="DateTime", parse_dates=True, delim_whitespace=True)
fig, ax= plt.subplots()
ax.plot_date(df.index, df.iloc[:,11], '-')
for i in range(10):
diff = df.index[1]-df.index[0]
spacing = diff/(1.3*len(df.columns))
ax.bar(df.index+(-5+i)*spacing, df.iloc[:,i],
width=spacing/diff, label=df.columns[i])
plt.legend()
plt.gcf().autofmt_xdate()
plt.show()
It will be possible to plot the bar plot and line plot in the same axes, if we neglect the fact that the points are dates. 如果我们忽略了点是日期这一事实,则可以在同一轴上绘制条形图和折线图。 In the following code mind that we do not read the first column as index . 在下面的代码中,请注意,我们不会将第一列读为index 。
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("data.csv", parse_dates=True, delim_whitespace=True)
ax = df.iloc[:,[0,1,2,3,4,5,6,7,8,9,10,11]].plot(kind='bar')
df.iloc[:,12].plot(linestyle='-', marker='o', ax = ax)
ax.set_xticklabels(df.DateTime, rotation=40)
plt.show()
So this method will produce a graph where the bars and linepoints are simply ordered by their index (which is not the date). 因此,此方法将生成一个图形,其中条形图和线点仅按其索引 (不是日期) 排序 。 This may be acceptable or not depending on whether the dates are equally spaced . 取决于日期是否间隔相等,这是否可以接受。
If we eg change the input file to skip a date (11/6/2014 is not present), the code will produce 如果例如将输入文件更改为跳过日期 (不存在2014年11 月6日 ),则代码将生成
where the bars and line points are still equally spaced, although the dates in reality are not. 尽管现实中的日期不是,但条形图和线点仍然等距分布。
Plotting the same data with one day skipped with the matplotlib code from the start of the answer we get 从得到答案的一开始,用matplotlib代码跳过一天就绘制相同的数据
where indeed the 11/6/2014 is missing. 确实缺少2014年11月6日的地方。
You just need to plot them on the same axis 您只需要在同一轴上绘制它们
ax = df.iloc[:,[0,1,2,3,4,5,6,7,8,9,10]].plot(kind='bar')
df.iloc[:,12].plot(linestyle='-', marker='o', ax = ax)
ax.set_xticklabels(df.DateTime, rotation=40) #set the x-ticks to datetime column and rotate
is the code I used to plot both the graphs on same plot 是我用来在同一图上绘制两个图的代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.