在同一图上绘制多个Excel张

发布于 2025-01-27 04:54:49 字数 1194 浏览 2 评论 0原文

我有我需要绘制的excel文件。到目前为止,我的代码看起来像这样

import pandas as pd 
import matplotlib.pyplot as plt

file = 'weatherdata.xlsx'


def plotMeteoData(file,x_axis,metric,*list_of_cities):
    
    df = pd.ExcelFile(file)
    sheets = df.sheet_names
    print(sheets)
    df_list = []
    for city in list_of_cities:
        df_list.append(df.parse(city))

    x=range(len(df_list[0][x_axis].tolist()))
    width = 0.3
    
    for j,df_item in enumerate(df_list):
        plt.bar(x,df_item[metric],width,label = sheets[j]) #this isn't working
        x = [i+width for i in x]
        
    
   
    plt.xlabel(x_axis)
    plt.ylabel(metric)
    
    plt.xticks(range(len(df_list[0][x_axis].tolist())),labels=df_list[0][x_axis].tolist())

    


    plt.show()
    
t=['Αθήνα','Θεσσαλονίκη','Πάτρα']
plotMeteoData(file,'Μήνας','Υγρασία',*t)

,并给出 this 输出。 每种颜色代表Excel板,X轴代表月份,Y轴代表某些值。 我已经评论了我试图为每张纸添加一些标签的行,但我无法。另外,如果您查看上述输出,则条形不会以每个XTICK为中心。我该如何解决这些问题?谢谢

I have this excel file that i need to plot. So far my code looks like this

import pandas as pd 
import matplotlib.pyplot as plt

file = 'weatherdata.xlsx'


def plotMeteoData(file,x_axis,metric,*list_of_cities):
    
    df = pd.ExcelFile(file)
    sheets = df.sheet_names
    print(sheets)
    df_list = []
    for city in list_of_cities:
        df_list.append(df.parse(city))

    x=range(len(df_list[0][x_axis].tolist()))
    width = 0.3
    
    for j,df_item in enumerate(df_list):
        plt.bar(x,df_item[metric],width,label = sheets[j]) #this isn't working
        x = [i+width for i in x]
        
    
   
    plt.xlabel(x_axis)
    plt.ylabel(metric)
    
    plt.xticks(range(len(df_list[0][x_axis].tolist())),labels=df_list[0][x_axis].tolist())

    


    plt.show()
    
t=['Αθήνα','Θεσσαλονίκη','Πάτρα']
plotMeteoData(file,'Μήνας','Υγρασία',*t)

and gives this output.
Each color represents an excel sheet, x-axis represents the months and y-axis represents some values.
I've commented the line where I'm trying to add some labels for each sheet and I'm unable to. Also if you look at the above output the bars aren't centered with each xtick. How can I fix those problems? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小…楫夜泊 2025-02-03 04:54:49

通常,您会使用plt.subplots,因为它使您可以更多地控制图形。下面的代码计算XTICK标签所需的偏移量,并用城市标签显示传奇:

import pandas as pd
import matplotlib.pyplot as plt

file = 'weatherdata.xlsx'


def plotMeteoData(file,x_axis,metric,*list_of_cities):
    df = pd.ExcelFile(file)
    sheets = df.sheet_names
    print(sheets)
    df_list = []
    for city in list_of_cities:
        df_list.append(df.parse(city))

    x=range(len(df_list[0][x_axis].tolist()))
    width = 0.3

    # Calculate the offset of the center of the xtick labels 
    xTickOffset = width*(len(list_of_cities)-1)/2
    
    # Create a plot
    fig, ax = plt.subplots()
    
    for j,df_item in enumerate(df_list):
        ax.bar(x,df_item[metric],width,label = sheets[j]) #this isn't working
        x = [i+width for i in x]

    ax.set_xlabel(x_axis)
    ax.set_ylabel(metric)

    # Add a legend (feel free to change the location)
    ax.legend(loc='upper right')

    # Add the xTickOffset to the xtick label positions so they are centered
    ax.set_xticks(list(map(lambda x:x+xTickOffset, range(len(df_list[0][x_axis].tolist())))),labels=df_list[0][x_axis].tolist())
    
    plt.show()
    
t=['Athena', 'Thessaloniki', 'Patras']
plotMeteoData(file,'Month','Humidity',*t)

结果图:

XTICK偏移量应考虑不同数量的Excel页面。参见 this 以获取有关传说的更多信息。

Typically you use plt.subplots, as it gives you more control over the graph. The code below calculates the offset needed for the xtick labels to be centered and shows the legend with the city labels:

import pandas as pd
import matplotlib.pyplot as plt

file = 'weatherdata.xlsx'


def plotMeteoData(file,x_axis,metric,*list_of_cities):
    df = pd.ExcelFile(file)
    sheets = df.sheet_names
    print(sheets)
    df_list = []
    for city in list_of_cities:
        df_list.append(df.parse(city))

    x=range(len(df_list[0][x_axis].tolist()))
    width = 0.3

    # Calculate the offset of the center of the xtick labels 
    xTickOffset = width*(len(list_of_cities)-1)/2
    
    # Create a plot
    fig, ax = plt.subplots()
    
    for j,df_item in enumerate(df_list):
        ax.bar(x,df_item[metric],width,label = sheets[j]) #this isn't working
        x = [i+width for i in x]

    ax.set_xlabel(x_axis)
    ax.set_ylabel(metric)

    # Add a legend (feel free to change the location)
    ax.legend(loc='upper right')

    # Add the xTickOffset to the xtick label positions so they are centered
    ax.set_xticks(list(map(lambda x:x+xTickOffset, range(len(df_list[0][x_axis].tolist())))),labels=df_list[0][x_axis].tolist())
    
    plt.show()
    
t=['Athena', 'Thessaloniki', 'Patras']
plotMeteoData(file,'Month','Humidity',*t)

Resulting Graph:
Resulting graph with random data

The xtick offset should account for different numbers of excel pages. See this for more information on legends.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文