用Python绘制同一excel文件中多张表的数据图

2024-06-16 13:26:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有4张表,它们有相同的列数和数据数。你知道吗

但是我想画一个压强为y,日期为X的曲线图,所以在一个曲线图中有四条线。我可以分开做,但不能全部做。这四张纸上的日期都是一样的,但每张纸上的金额可能不同价值观。那个您可以在我的代码中找到一个名称,它有助于每次只选择一个压力。我需要选择这些来制作新的工作表吗?或者有没有其他方法来制作这个情节?你知道吗

以下是我的单张图的代码:

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文件(此处无法制表,请描述):

每张纸有两列,日期和压力。你知道吗


Tags: 代码nameimportpandasdatatimeplotis
2条回答

我没有你的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()

我假设你正在尝试合并和绘制来自四张纸的所有数据。在这种情况下,您可以将每个工作表中的数据加载到一个文件中,并将它们合并在一起。考虑到您的工作表具有相同的数据量,这会变得更容易。 您可以直接使用熊猫:

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英尺

相关问题 更多 >