简体   繁体   中英

How to make a plot of data from several sheets in same excel file by using Python pandas

I have 4 sheets, they have same numbers of columns and same numbers of data.

But I want to make a plot of pressure as y while date as X. So there will be four lines in one graph. I can make them separately but not all together. The date is all same in the four sheets but for each date, each sheet may have different amount values.That's something you can find in my codes about is_basin_name to help to select only one pressure for each time. Do I need to select these to make a new sheet? Or is there an alternative way to make this plot?

Here are the codes of my single sheet's plot:

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()

The excel file (cannot make a table here, so describe):

There are two columns for each sheet, date and pressure.

I'm assuming you are trying to merge and plot all the data coming from the four sheets. In this case, you could load data from each sheet into a pandas df and merge them together. This is made easier given that your sheets have the same amount of data. You can use pandas directly:

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 : see first example.

By the way, what does this mean? 'Date pressure 1. 1 2. 2 3. 2 4. 2'

I don't have your excel file so you'll need to test it yourself.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM