2017-04-20 77 views
2

我已經從模擬中獲得數據,該數據給了我存儲在DataFrame中的一些值(100行x 6列)。對於不同的起始值,我將數據保存在面板中(2個DataFrames×100行×6列)。從面板中的各種數據框繪製同一列

現在我想比較評爲「A」在兩個模擬(DataFrames命名爲「SIM1」和「SIM2」)列如何比較和做的一個方法是通過DataFrame.plot命令

Panel['Sim1'].plot(x = 'xvalues', y='A') 
Panel['Sim2'].plot(x = 'xvalues', y='A') 
plt.show() 

這工作,但我覺得它在某種程度上應該是可以繪製像大數據是相同的數據幀在那裏我可以畫出這樣

DataFrame.plot(x = 'xvalues', y = ['A1', 'A2']) 

我缺少的東西,或只是可能僅僅積兩如果數據存儲在面板中,則用一個命令將其繪製成一個圖形?

+0

請印刷後的'輸出(面板)'' – MaxU

+0

<類的pandas.core.panel.Panel「 > 尺寸:2(項)×101(major_axis)×5(minor_axis) 物品軸:1至2個 major_axis軸:0〜100個 Minor_axis軸:x至A4' – theGwiel

回答

1

考慮下面的例子:

In [77]: import pandas_datareader.data as web 

In [78]: p = web.DataReader(['AAPL','GOOGL'], 'yahoo', '2017-01-01') 

In [79]: p.axes 
Out[79]: 
[Index(['Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close'], dtype='object'), 
DatetimeIndex(['2017-01-03', '2017-01-04', '2017-01-05', '2017-01-06', '2017-01-09', '2017-01-10', '2017-01-11', '2017-01-12', 
       '2017-01-13', '2017-01-17', '2017-01-18', '2017-01-19', '2017-01-20', '2017-01-23', '2017-01-24', '2017-01-25', 
       '2017-01-26', '2017-01-27', '2017-01-30', '2017-01-31', '2017-02-01', '2017-02-02', '2017-02-03', '2017-02-06', 
       '2017-02-07', '2017-02-08', '2017-02-09', '2017-02-10', '2017-02-13', '2017-02-14', '2017-02-15', '2017-02-16', 
       '2017-02-17', '2017-02-21', '2017-02-22', '2017-02-23', '2017-02-24', '2017-02-27', '2017-02-28', '2017-03-01', 
       '2017-03-02', '2017-03-03', '2017-03-06', '2017-03-07', '2017-03-08', '2017-03-09', '2017-03-10', '2017-03-13', 
       '2017-03-14', '2017-03-15', '2017-03-16', '2017-03-17', '2017-03-20', '2017-03-21', '2017-03-22', '2017-03-23', 
       '2017-03-24', '2017-03-27', '2017-03-28', '2017-03-29', '2017-03-30', '2017-03-31', '2017-04-03', '2017-04-04', 
       '2017-04-05', '2017-04-06', '2017-04-07', '2017-04-10', '2017-04-11', '2017-04-12', '2017-04-13', '2017-04-17', 
       '2017-04-18', '2017-04-19', '2017-04-20', '2017-04-21'], 
       dtype='datetime64[ns]', name='Date', freq=None), 
Index(['AAPL', 'GOOGL'], dtype='object')] 

In [80]: p.loc['Adj Close'] 
Out[80]: 
        AAPL  GOOGL 
Date 
2017-01-03 115.648597 808.010010 
2017-01-04 115.519154 807.770020 
2017-01-05 116.106611 813.020020 
2017-01-06 117.401002 825.210022 
2017-01-09 118.476334 827.179993 
2017-01-10 118.595819 826.010010 
2017-01-11 119.233055 829.859985 
2017-01-12 118.735214 829.530029 
2017-01-13 118.526121 830.940002 
2017-01-17 119.481976 827.460022 
...    ...   ... 
2017-04-07 143.339996 842.099976 
2017-04-10 143.169998 841.700012 
2017-04-11 141.630005 839.880005 
2017-04-12 141.800003 841.460022 
2017-04-13 141.050003 840.179993 
2017-04-17 141.830002 855.130005 
2017-04-18 141.199997 853.989990 
2017-04-19 140.679993 856.510010 
2017-04-20 142.440002 860.080017 
2017-04-21 142.270004 858.950012 

[76 rows x 2 columns] 

情節它

In [81]: p.loc['Adj Close'].plot() 
Out[81]: <matplotlib.axes._subplots.AxesSubplot at 0xdabfda0> 

enter image description here

不同SL的實例除冰/索引/選擇用於所述樣品面板:

In [118]: p 
Out[118]: 
<class 'pandas.core.panel.Panel'> 
Dimensions: 6 (items) x 76 (major_axis) x 2 (minor_axis) 
Items axis: Open to Adj Close 
Major_axis axis: 2017-01-03 00:00:00 to 2017-04-21 00:00:00 
Minor_axis axis: AAPL to GOOGL 

通過項軸線(指數):

In [119]: p.loc['Adj Close'] 
Out[119]: 
        AAPL  GOOGL 
Date 
2017-01-03 115.648597 808.010010 
2017-01-04 115.519154 807.770020 
2017-01-05 116.106611 813.020020 
2017-01-06 117.401002 825.210022 
2017-01-09 118.476334 827.179993 
2017-01-10 118.595819 826.010010 
2017-01-11 119.233055 829.859985 
2017-01-12 118.735214 829.530029 
2017-01-13 118.526121 830.940002 
2017-01-17 119.481976 827.460022 
...    ...   ... 
2017-04-07 143.339996 842.099976 
2017-04-10 143.169998 841.700012 
2017-04-11 141.630005 839.880005 
2017-04-12 141.800003 841.460022 
2017-04-13 141.050003 840.179993 
2017-04-17 141.830002 855.130005 
2017-04-18 141.199997 853.989990 
2017-04-19 140.679993 856.510010 
2017-04-20 142.440002 860.080017 
2017-04-21 142.270004 858.950012 

[76 rows x 2 columns] 

通過長軸:

In [120]: p.loc[:, '2017-01-03'] 
Out[120]: 
      Open  High   Low  Close  Volume Adj Close 
AAPL 115.800003 116.330002 114.760002 116.150002 28781900.0 115.648597 
GOOGL 800.619995 811.440002 796.890015 808.010010 1959000.0 808.010010 

通過短軸:

In [121]: p.loc[:, :, 'GOOGL'] 
Out[121]: 
        Open  High   Low  Close  Volume Adj Close 
Date 
2017-01-03 800.619995 811.440002 796.890015 808.010010 1959000.0 808.010010 
2017-01-04 809.890015 813.429993 804.109985 807.770020 1515300.0 807.770020 
2017-01-05 807.500000 813.739990 805.919983 813.020020 1340500.0 813.020020 
2017-01-06 814.989990 828.960022 811.500000 825.210022 2017100.0 825.210022 
2017-01-09 826.369995 830.429993 821.619995 827.179993 1406800.0 827.179993 
2017-01-10 827.070007 829.409973 823.140015 826.010010 1194500.0 826.010010 
2017-01-11 826.619995 829.900024 821.469971 829.859985 1320200.0 829.859985 
2017-01-12 828.380005 830.380005 821.010010 829.530029 1349500.0 829.530029 
2017-01-13 831.000000 834.650024 829.520020 830.940002 1288000.0 830.940002 
2017-01-17 830.000000 830.179993 823.200012 827.460022 1439700.0 827.460022 
...    ...   ...   ...   ...  ...   ... 
2017-04-07 845.000000 845.880005 837.299988 842.099976 1110000.0 842.099976 
2017-04-10 841.539978 846.739990 840.789978 841.700012 1021200.0 841.700012 
2017-04-11 841.700012 844.630005 834.599976 839.880005 971900.0 839.880005 
2017-04-12 838.460022 843.719971 837.590027 841.460022 1126100.0 841.460022 
2017-04-13 841.039978 843.729980 837.849976 840.179993 1067200.0 840.179993 
2017-04-17 841.380005 855.640015 841.030029 855.130005 1044800.0 855.130005 
2017-04-18 852.539978 857.390015 851.250000 853.989990 935200.0 853.989990 
2017-04-19 857.390015 860.200012 853.530029 856.510010 1077500.0 856.510010 
2017-04-20 859.739990 863.929993 857.500000 860.080017 1186900.0 860.080017 
2017-04-21 860.619995 862.440002 857.729980 858.950012 1168200.0 858.950012 

[76 rows x 6 columns] 

在你的情況(取決於您的軸),你可能希望以不同的切片面板:

Panel.loc[:, :, 'A'].plot() 
+0

當然,我沒有想到,不是每次模擬都使用完整的DataFrame和所有數據,我可以使用次軸來確定模擬計數,從而生成一個Panel。完善!現在我只需要找到一種方法來優雅地創建這樣一個面板並添加到它。我之前的做法是計算每個時間步長的每個值,並將這個時間步長追加到我現有的DataFrame中以進行這種非常模擬。這仍然有效,但是我相比於一個numpy數組贏不了任何東西,因爲我必須爲每個DataFrame(yvalues A1到A4)追加自己 - 甚至更多的yvalues? – theGwiel

+0

我做了,但因爲我有不到15點,它沒有出現公衆 \編輯:不知道接受選項 - 謝謝:) – theGwiel

+0

@theGwiel,謝謝你接受一個答案!如果你打開一個帶有示例數據集的新問題,請從這個問題ping我 - 我會嘗試提供一個工作示例 – MaxU

0

以下是一種使用Panel.apply()的方法。
apply(plt.plot)的輸出是Line2D對象的數據幀的minor_axis-itemsplot()試圖繪製一個對我們的目的沒有意義的額外維度,但我們可以使用lines.pop()來刪除違規維度。希望這可以幫助。

# generate sample data 
x = np.arange(20) 
y1 = np.random.randint(100, size=20) 
y2 = np.random.randint(100, size=20) 
data = {'A1': pd.DataFrame({'y':y1,'x':x}), 
     'A2': pd.DataFrame({'y':y2,'x':x})} 
p = pd.Panel(data) 

# plot panels 
df = p.apply(plt.plot) 
df.ix[0,0].axes.lines.pop(2) 
df.ix[0,0].axes.lines.pop(0) 
df.ix[0,0].axes.legend(loc="lower right") 

panel plot

+0

這是解決一個奇怪的方式問題,特別是在處理多列時......在我的情況下,您的方法似乎是012f'df.ix [0,0] .axes.lines.pop(1).pop(2).pop(3)。 pop(4)' ? – theGwiel

相關問題