【发布时间】:2014-09-27 03:57:14
【问题描述】:
我有一个与此处发布的问题类似的问题: Multiple data set plotting with matplotlib.pyplot.plot_date
这对我有用,但我想在同一个图上绘制两个以上的图。
在我的例子中,例如,如果我调用 plot_date() 函数 5 次,结果图显示最后两次调用的点/线,但前三个调用,线没有绘制但所有 5 出现在图例中(我在 5 次调用中分别用不同的颜色和标签来区分)。
概述是我正在使用python,将带有数据(系列标签、日期、计数(y))的csv文本文件打开到一个元组列表中,然后将该列表放入一个pandas数据框中。然后我将其旋转以将其更改为
df = df.pivot(index='date', columns='series', values='count')
然后是我的绘图代码:
fig = plt.figure()
plt.plot_date(x=df.index, y=df['series1'], fmt='bo-', tz=None, xdate=True,
ydate=False, label="d1", color='red')
plt.plot_date(x=df.index, y=df['series2'], fmt='bo-', tz=None, xdate=True,
ydate=False, label="d2", color='blue')
plt.plot_date(x=df.index, y=df['series3'], fmt='bo-', tz=None, xdate=True,
ydate=False, label="d3", color='green')
plt.plot_date(x=df.index, y=df_date_domain['series4'], fmt='bo-', tz=None, xdate=True,
ydate=False, label="d4", color='orange')
plt.plot_date(x=df.index, y=df_date_domain['series5'], fmt='bo-', tz=None, xdate=True,
ydate=False, label="d5", color='black')
fig.autofmt_xdate()
plt.legend()
plt.xlabel("Day")
plt.ylabel("Count")
plt.title("example of trying to plot more than 2 on the same figure")
fname='test.pdf'
plt.savefig(fname)
下面是结果
下面是完整的代码,后面是文本输入(python test_plot.py plot_test.csv)
import sys
import pandas as pd
from ggplot import *
import matplotlib.pyplot as plt
def main(argv=sys.argv):
if len(sys.argv) != 2:
print sys.argv[0], "CSVinputFile (path if not in current dir)"
sys.exit(-2)
inFileName = sys.argv[1]
qname_list = []
print inFileName
with open(inFileName, 'Ur') as fp:
data_list = [tuple(line.strip().split(",")) for line in fp]
header_row=['series','date','count']
df = pd.DataFrame.from_records(data_list,columns=header_row)
df['date'] = pd.to_datetime(df['date'])
print df.head(10)
df = df.pivot(index='date', columns='series', values='count')
print df.head(10)
print df.describe()
#extract the columns out of the data to plot out
series_2_extract = ['series1', 'series3', 'series2']
#d_data = df[[series_2_extract]] #doesnt work TypeError: unhashable type: 'list'
d_data = df[['series1', 'series3', 'series2']]
print d_data
#below works, can use a loop to iterate the list and call plot_date for each item in the list,
#but only last two series are showing on the plot
fig = plt.figure()
plt.plot_date(x=df.index, y=df['series1'], fmt='bo-', tz=None, xdate=True,
ydate=False, label="d1", color='red')
plt.plot_date(x=df.index, y=df['series2'], fmt='bo-', tz=None, xdate=True,
ydate=False, label="d2", color='blue')
plt.plot_date(x=df.index, y=df['series3'], fmt='bo-', tz=None, xdate=True,
ydate=False, label="d3", color='green')
plt.plot_date(x=df.index, y=df['series4'], fmt='bo-', tz=None, xdate=True,
ydate=False, label="d4", color='orange')
plt.plot_date(x=df.index, y=df['series5'], fmt='bo-', tz=None, xdate=True,
ydate=False, label="d5", color='black')
fig.autofmt_xdate()
plt.legend()
plt.xlabel("Day")
plt.ylabel("Count")
plt.title("example of trying to plot more than 2 on the same figure")
fname='test.pdf'
plt.savefig(fname)
return 0
if __name__ == '__main__':
sys.exit(main())
由于文本输入很长,我在pastebin这里有 http://pastebin.com/hmCUabvu 以上代码也在pastebin:http://pastebin.com/07TNYie4
【问题讨论】:
-
您能否提供一个可运行的示例,其中包含演示问题的示例数据?
-
@BrenBarn - 我添加了代码,以及指向数据输入和相同代码的 pastebin 链接。谢谢
标签: python matplotlib