2016-08-15 35 views
0

我有4張圖表,它們有相同數量的列和相同數量的數據。如何使用Python熊貓在同一個excel文件中製作多張數據圖

但是我想把壓力圖作爲y,而將日期作爲X.所以在一張圖中將會有四條線。我可以分開製作它們,但不是全部。日期在四張表中都是相同的,但對於每個日期,每張表可能有不同的金額值。這是您可以在我的關於is_basin_name的代碼中找到的,以幫助每次僅選擇一個壓力。我是否需要選擇這些來製作新的工作表?或者還有其他方法可以製作這個情節?

這裏是我的單頁紙的劇情的代碼:

import pandas as pd 
import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
a=pd.read_excel('mslp0.0001.xlsx', '0.1-20', index_col=None, na_values=['NA']) 
c=[] 
c=a[['basin name','lead time(hours)','max 10-m wind 9kt0','min MSLP(hPa)','wind speed threshold(kt)']] 
is_basin_name = a['lat*10'] > 0 
is_wind_thresh =a['wind speed threshold(kt)'] == 34 
#data to make a plot of mslp and 10m wind with leading time 
valid_data = a[is_basin_name & is_wind_thresh] 
#plot of mslp and lead time 
ax=valid_data.plot(kind='line',x='lead time(hours)',y='min MSLP(hPa)') 
plt.show() 

Excel文件(不能在這裏做一個表,所以描述):

有兩列每個工作表,日期和壓力。

+0

雖然我不熟悉的大熊貓,我已經使用過http://xlsxwriter.readthedocs.io(http://xlsxwriter.readthedocs.io/)以編程方式創建XLSX表和圖表。 –

回答

0

我假設您正在嘗試合併並繪製來自四張工作表的所有數據。在這種情況下,您可以將每張表中的數據加載到熊貓df中,並將它們合併在一起。考慮到您的工作表具有相同數量的數據,這會變得更容易。 您可以直接使用熊貓:

import pandas as pd 
data = pd.ExcelFile(".../pressure_data.xlsx") 

list_dfs = [] 
sheets = data.sheet_names 
for sheet in sheets: 
    list_dfs = data.parse(sheet) 

df = pd.concat(list_dfs) 

#plot 

pandas concat:看第一個例子。

順便說一下,這是什麼意思? '日期壓力1. 1 2. 2 3. 2 4. 2'

+0

我很着急,沒有做出好的格式..它應該是兩列:) –

+0

我用更具體的細節描述了我的問題,你可以再看一下嗎?這將有很大幫助! –

0

我沒有你的excel文件,所以你需要自己測試一下。

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 

#sheetname=None will load all sheets in to a dict of dataframes 
dict_of_df = pd.read_excel('mslp0.0001.xlsx', sheetname=None, index_col=None, na_values=['NA']) 

ax = None 
for sheet, df in dict_of_df.iteritems(): 
    is_basin_name = df['lat*10'] > 0 
    is_wind_thresh = df['wind speed threshold(kt)'] == 34 
    #data to make a plot of mslp and 10m wind with leading time 
    valid_data = df[is_basin_name & is_wind_thresh] 
    #plot of mslp and lead time 
    ax = valid_data.plot(kind='line',x='lead time(hours)',y='min MSLP(hPa)', ax=ax, label=sheet) #ax=ax will re-use the same ax for plotting so your lines will be in the same chart. When hitting this line for the first time, ax=None so a new chart will be created. Use label=sheet to identify which line is from which sheet. 
plt.legend(loc=0) #probably legend is shown by default already. 
plt.show() 
相關問題