【问题标题】:How to plot each year as a line with months on the x-axis如何将每年绘制为 x 轴上月份的一条线
【发布时间】:2021-12-29 03:33:18
【问题描述】:

我有一个问题,我有以下数据框,其中包含多年和数月的总和:

import pandas as pd

data = {'Bedrag': [210406.49, 191369.87, 118458.65, 81682.95, 90571.61, 196374.53, 223619.85, 144773.64, 240221.67, 110666.73, 108633.49, 194808.85, 103302.85, 186419.17, 96297.53, 81404.79, 94874.21, 209520.46, 270694.15, 107448.21, 188290.77, 163761.78, 168799.28, 190937.74, 127930.28, 262299.96, 48658.0, 48027.57, 220501.67, 234570.63, 89188.45, 233270.46, 179647.23, 272358.86],
        'Jaar': [2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021],
        'maand': ['April', 'August', 'December', 'February', 'January', 'July', 'June', 'March', 'May', 'November', 'October', 'September', 'April', 'August', 'December', 'February', 'January', 'July', 'June', 'March', 'May', 'November', 'October', 'September', 'April', 'August', 'February', 'January', 'July', 'June', 'March', 'May', 'October', 'September']}

correct_omzet = pd.DataFrame(data)

# display(correct_omzet.head())

      Bedrag  Jaar     maand
0  210406.49  2019     April
1  191369.87  2019    August
2  118458.65  2019  December
3   81682.95  2019  February
4   90571.61  2019   January

现在我想将 seaborn 绘制成线图,以查看季节销售模式。但是当我尝试如下绘制时,结果只显示了 12 个月。

plt.figure(figsize=(25,6))
sns.set_style("darkgrid")
sns.lineplot(data=correct_omzet, x="maand", y="Bedrag", ci=None, color="green", marker='o')

我也尝试过在没有 seaborn 的情况下绘制它:

correct_omzet.plot.line(x='maand', rot=90, y='Bedrag', title='Omzetpatroon 2019 - 2021', colormap='Spectral', figsize=(20,8))
plt.minorticks_on()

但是我无法弄清楚将所有月份都作为 x 轴标题。

【问题讨论】:

  • 希望答案是有帮助的。彻底回答问题很费时间。如果您的问题已解决,请接受解决方案 位于答案左上角的 ▲/▼ 箭头下方。如果出现更好的解决方案,则可以接受新的解决方案。如果您的声望超过 15,您还可以使用 ▲/▼ 箭头对答案的有用性进行投票。 如果解决方案无法回答问题,请发表评论。 What should I do when someone answers my question?。谢谢。

标签: python pandas dataframe plot seaborn


【解决方案1】:
  • 进行季节性观察的最清晰方法是将每年绘制为一条单独的线,x 轴为月份
  • 使用pd.Categorical 将月份列设置为有序分类,这将确保按顺序绘制月份。内置的calendar包用于有序的month_name列表,或者手动创建列表。
  • 使用图形级别的seaborn.relplotkind='line' 绘制数据并使用hue= 参数分隔'Jaar'
    • 使用图形级绘图是因为它有heightaspect 用于调整大小,无需创建fig, ax = plt.subplots(figsize=(5, 10))。否则使用坐标区级别的seaborn.lineplot
  • 另见Weather Visualization for Portland, OR: 1940 - 2020
  • python 3.8.12pandas 1.3.4matplotlib 3.4.3seaborn 0.11.2 中测试
import pandas as pd
import seaborn as sns
from calendar import month_name as mn

# month list
months = mn[1:]

# convert the column to categorical and ordered
correct_omzet.maand = pd.Categorical(correct_omzet.maand, categories=months, ordered=True)

# plot the data
p = sns.relplot(kind='line', data=correct_omzet, x='maand', y='Bedrag', hue='Jaar', aspect=2.5, marker='o')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多