Python Pandas GroupBy 并排绘制折线图和条形图(在一张图像中)

问题描述 投票:0回答:1

我想将它们(从 GroupBy)绘制成并排的折线图和条形图(在 1 张图像中)的不同列的数据框。

下面几行生成了 2 个单独的图表,我尝试了但仍然无法将它们放入侧面 1 图像中。

import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from io import StringIO

csvfile = StringIO(
"""
Name    Year - Month    Score   Thumbs-up
Mike    2022-09 192 5
Mike    2022-08 708 5
Mike    2022-07 140 3
Mike    2022-05 144 8
Mike    2022-04 60  10
Mike    2022-03 108 4
Kate    2022-07 19850   5
Kate    2022-06 19105   2
Kate    2022-05 23740   3
Kate    2022-04 19780   9
Kate    2022-03 15495   4 """)

df = pd.read_csv(csvfile, sep = '\t', engine='python')

for group_name, sub_frame in df.groupby("Name"):

    fig, axes = plt.subplots(nrows=1,ncols=2,figsize=(12,6))"

    sub_frame_sorted = sub_frame.sort_values('Year - Month')       # sort the data-frame by a column"

    line_chart = sub_frame_sorted.plot(""Year - Month"", ""Score"", legend=False)"
    bar_chart = sub_frame_sorted.plot.bar(""Year - Month"", ""Thumbs-up"", legend=False)"

    # for data labeling in the charts
    i=0
    for ix, vl in sub_frame_sorted.iterrows():
        line_chart.annotate(vl['Score'], (i, vl['Score']), ha='center')
        bar_chart.annotate(vl['Thumbs-up'], (i, vl['Thumbs-up']), ha='center')
        i=i+1

    plt.show()

这样做的正确方法是什么(如果 matplotlib 可以这样做)?谢谢。

python pandas matplotlib plot charts
1个回答
1
投票

是的,matplotlib 可以做到这一点。现在,我不得不稍微修改一下你的 csv 输入,我认为你应该在使用该方法之前格式化你的数据,但你正在寻找一种绘图方式,所以我希望你不会不同意这种格式。

import matplotlib.pyplot as plt
import pandas as pd
from io import StringIO

csvfile = StringIO(
"""
Name;Year-Month;Score;Thumbs-up
Mike;2022-09;192;5
Mike;2022-08;708;5
Mike;2022-07;140;3
Mike;2022-05;144;8
Mike;2022-04;60;10
Mike;2022-03;108;4
Kate;2022-07;19850;5
Kate;2022-06;19105;2
Kate;2022-05;23740;3
Kate;2022-04;19780;9
Kate;2022-03;15495;4 """)

df = pd.read_csv(csvfile, sep = ';', engine='python')
print(df)
fig, axes = plt.subplots(nrows=1,ncols=2,figsize=(12,6))

for group_name, sub_frame in df.groupby("Name"):

    sub_frame_sorted = sub_frame.sort_values('Year-Month')       # sort the data-frame by a column"

    sub_frame_sorted.plot(ax=axes[0], x="Year-Month", y="Score", label=group_name)
    sub_frame_sorted.plot(ax=axes[1], kind='bar', x="Year-Month", y="Thumbs-up", label=group_name)

    for i, (ix, vl) in enumerate(sub_frame_sorted.iterrows()):
        axes[0].annotate(vl['Score'], (i, vl['Score']), ha='center')
        axes[1].annotate(vl['Thumbs-up'], (i, vl['Thumbs-up']), ha='center')

axes[0].set_xlabel('Year-Month')
axes[0].set_ylabel('Score')
axes[0].legend()

axes[1].set_xlabel('Year-Month')
axes[1].set_ylabel('Thumbs-up')
axes[1].legend()

plt.show()

将返回:

编辑另一种选择是

import matplotlib.pyplot as plt
import pandas as pd
from io import StringIO

csvfile = StringIO(
"""
Name;Year-Month;Score;Thumbs-up
Mike;2022-09;192;5
Mike;2022-08;708;5
Mike;2022-07;140;3
Mike;2022-05;144;8
Mike;2022-04;60;10
Mike;2022-03;108;4
Kate;2022-07;19850;5
Kate;2022-06;19105;2
Kate;2022-05;23740;3
Kate;2022-04;19780;9
Kate;2022-03;15495;4 """)

df = pd.read_csv(csvfile, sep = ';', engine='python')
print(df)
fig, axes = plt.subplots(nrows=1,ncols=2,figsize=(12,6))

for group_name, sub_frame in df.groupby("Name"):

    sub_frame_sorted = sub_frame.sort_values('Year-Month')       # sort the data-frame by a column"

    sub_frame_sorted.plot(ax=axes[0], x="Year-Month", y="Score", label=group_name)
    sub_frame_sorted.plot(ax=axes[1], kind='bar', x="Year-Month", y="Thumbs-up", label=group_name)

    xticks = sub_frame_sorted["Year-Month"][::2].tolist()  # only include every other x-axis label
    for i, (ix, vl) in enumerate(sub_frame_sorted.iterrows()):
        axes[0].annotate(vl['Score'], (i, vl['Score']), ha='center')
        axes[1].annotate(vl['Thumbs-up'], (i, vl['Thumbs-up']), ha='center')

    axes[0].set_xticks(sub_frame_sorted.index[::2])
    axes[0].set_xticklabels(xticks, rotation=45)
    axes[1].set_xticks(sub_frame_sorted.index[::2])
    axes[1].set_xticklabels(xticks, rotation=45)

axes[0].set_xlabel('Year-Month')
axes[0].set_ylabel('Score')
axes[0].legend()

axes[1].set_xlabel('Year-Month')
axes[1].set_ylabel('Thumbs-up')
axes[1].legend()

plt.show()

这给

© www.soinside.com 2019 - 2024. All rights reserved.