我对此处发布的问题提出了类似的问题: Multiple data set plotting with matplotlib.pyplot.plot_date
这对我有用,但我想在同一个数字上绘制2个以上的情节。
在我的情况下,例如,如果我调用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
答案 0 :(得分:1)
因为数据是一样的。你的线条相互重叠。
>>> np.all(df['series1'] == df['series5'])
True
>>> np.all(df['series1'] == df['series3'])
True
>>> np.all(df['series2'] == df['series4'])
True