绘制数据框

时间:2015-10-31 00:47:08

标签: python pandas plot

我有一个这样的数据框:

ReviewDate_month,ProductId,Reviewer
01,185,185
02,155,155
03,130,130
04,111,111
05,110,110
06,98,98
07,101,92
08,71,71
09,73,73
10,76,76
11,105,105
12,189,189

我想绘制它,X中的ReviewDate_Month,理想的Y中的产品ID和审阅者。但我将从1行开始,无论是产品ID还是审稿人。 所以我试过了:

df_no_monthlycount.plot.line

得到以下错误消息:

File "C:/Users/user/PycharmProjects/Assign2/Main.py", line 59, in <module>
01                      185       185
02                      155       155
03                      130       130
04                      111       111
05                      110       110
06                       98        98
07                      101        92
08                       71        71
09                       73        73
10                       76        76
    df_no_monthlycount.plot.line
AttributeError: 'function' object has no attribute 'line'
11                      105       105
12                      189       189

Process finished with exit code 1

我也试过这个:

df_no_monthlycount.plot(x=df_helful_monthlymean['ReviewDate_month'],y=df_helful_monthlymean['ProductId'],style='o')

错误信息如下:

Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/Assign2/Main.py", line 52, in <module>
    df_no_monthlycount.plot(x=df_helful_monthlymean['ReviewDate_month'],y=df_helful_monthlymean['ProductId'],style='o')
  File "C:\Python34\lib\site-packages\pandas\core\frame.py", line 1797, in __getitem__
    return self._getitem_column(key)
  File "C:\Python34\lib\site-packages\pandas\core\frame.py", line 1804, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Python34\lib\site-packages\pandas\core\generic.py", line 1084, in _get_item_cache
    values = self._data.get(item)
  File "C:\Python34\lib\site-packages\pandas\core\internals.py", line 2851, in get
    loc = self.items.get_loc(item)
  File "C:\Python34\lib\site-packages\pandas\core\index.py", line 1572, in get_loc
    return self._engine.get_loc(_values_from_object(key))
  File "pandas\index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas\index.c:3838)
  File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:3718)
  File "pandas\hashtable.pyx", line 686, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12294)
  File "pandas\hashtable.pyx", line 694, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12245)
KeyError: 'ReviewDate_month'

2 个答案:

答案 0 :(得分:1)

如下所示调用plot

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('data.csv')

print(df)

df.plot(x ='ReviewDate_month',y=['ProductId', 'Reviewer'] ,kind='line')
plt.show()

会给你:

enter image description here

答案 1 :(得分:0)

如果您想在import matplotlib.pyplot as plt vals = [1, 100, 14, 76, 33] # random data, aligned to `x_indices` fig, ax1 = plt.subplots(1,1) ax1.plot(x_indices, vals) ax2 = ax1.twiny() # Remark: twiny will create a new axes # where the y-axis is shared with ax1, # but the x-axis is independant - important! ax2.set_xlim(ax1.get_xlim()) # ensure the independant x-axes now span the same range ax2.set_xticks(x_indices) # copy over the locations of the x-ticks from the first axes ax2.set_xticklabels(x_percents) # But give them a different meaning ReviewDate_MonthXProduct ID Reviewer中绘制Y,可以这样做:

df_no_monthlycount.plot(x='ReviewDate_Month', y=['Product ID', 'Reviewer'])