pandas 为什么我有6个图表而不是5个?

yx2lnoni  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(102)

我有这个函数plotDataFrames

df=pd.read_csv('dataset_SCL.csv', parse_dates=parse_dates, low_memory= False)

def bar_plot_vuelos(var1: str, var2: str, plot_type: str, plot_title: str, y_label: str, x_label: str) -> pd.DataFrame.plot :
        """
        Return bar plot.

        """
        df_groups=df.groupby(var1)[var2].sum()
        df_groups.plot(kind=plot_type)
        bar_plot=df_groups.plot(kind=plot_type, title=plot_title ,ylabel=y_label, xlabel=x_label, figsize=(25, 9),stacked= True)
        return bar_plot

This function works fine. The problem is when I try to make 3 graphs, I can see only 1 (the last one)

plot1=bar_plot_vuelos('SIGLADES', 'Atraso', 'bar', 'Cantidad de atrasos por destino', 'Cantidad de atrasos', 'Destino')
plot2=bar_plot_vuelos('OPERA', 'Atraso', 'bar', 'Atraso por aerolíneas', 'Cantidad de atrasos', 'Aerolinea')
plot3=bar_plot_vuelos('MES', 'Atraso', 'bar', 'Cantidad de atrasos por mes', 'Cantidad de atrasos', 'Meses')

我想要的:显示所有3个图
编辑解决:我添加了plt.figure(),原来的问题是:"为什么df. plot只显示最后一个图"?

def bar_plot_vuelos(var1: str, var2: str, plot_type: str, plot_title: str, y_label: str, x_label: str) -> pd.DataFrame.plot :
    """
    Return bar plot.

    """
    df_groups=df.groupby(var1)[var2].sum()
    plt.figure()
    df_groups.plot(kind=plot_type)
    bar_plot=df_groups.plot(kind=plot_type, title=plot_title ,ylabel=y_label, xlabel=x_label, figsize=(25, 9),stacked= True)
    return bar_plot

这将为3个示例创建一个地物。

plot1=bar_plot_vuelos('SIGLADES', 'Atraso', 'bar', 'Cantidad de atrasos por destino', 'Cantidad de atrasos', 'Destino')
plot2=bar_plot_vuelos('OPERA', 'Atraso', 'bar', 'Atraso por aerolíneas', 'Cantidad de atrasos', 'Aerolinea')
plot3=bar_plot_vuelos('MES', 'Atraso', 'bar', 'Cantidad de atrasos por mes', 'Cantidad de atrasos', 'Meses')
fwzugrvs

fwzugrvs1#

你要第一个还是最后一个
我看到的是你在同一个Figure中绘图,这覆盖了每一个绘图,我复制了它,总是得到最后一个。
如果你想在同一张图中显示所有的3个图,我建议使用多个Axes,每个图一个。

df=pd.read_csv('dataset_SCL.csv', parse_dates=parse_dates, low_memory= False)

def bar_plot_vuelos(
    var1: str,
    var2: str,
    plot_type: str,
    plot_title: str,
    y_label: str,
    x_label: str,
    ax=plt.gca(),  # Include a parameter to indicate the 'Axes' instance
) -> pd.DataFrame.plot:
    """
    Return bar plot.

    """
    df_groups = df.groupby(var1)[var2].sum()
    bar_plot = df_groups.plot(
        kind=plot_type,
        title=plot_title,
        ylabel=y_label,
        xlabel=x_label,
        stacked=True,
        ax=ax,  # to use here
    )

    return bar_plot

fig, axes = plt.subplots(
    nrows=3, figsize=(25, 3 * 9)
)  # Create 'axes': List of 'Axes' objects

plot_parameters = [
    ("SIGLADES", "Cantidad de atrasos por destino", "Destino"),
    ("OPERA", "Cantidad de atrasos por aerolínea", "Aerolínea"),
    ("MES", "Cantidad de atrasos por destino", "Meses"),
]

for parameters_tuple, ax in zip(
    plot_parameters, axes
):  # Zip each plot's parameter to each 'Axes'
    var1 = parameters_tuple[0]
    plot_title = parameters_tuple[1]
    x_label = parameters_tuple[2]

    bar_plot_vuelos(
        var1=var1,
        var2="Atraso",
        plot_type="bar",
        plot_title=plot_title,
        y_label="Cantidad de atrasos",
        x_label=x_label,
        ax=ax,
    )

相关问题