简体   繁体   English

在 x 轴上为日历周 YYYYWW 绘制 Seaborn 多条线图

[英]Seaborn multiple lineplots for calendar weeks YYYYWW on x-axis

I have some problems with the x-axis values of a seaborn line-plot:我对 seaborn 线图的 x 轴值有一些问题:

import pandas as pd
import seaborn as sns

# data
df = pd.DataFrame(columns=['calendar_week', 'product_name', 'value'],
                  data=[['201850', 'product01', 1], ['201905', 'product01', 10], ['201910', 'product01', 7],
                       ['201840', 'product02', 4], ['201911', 'product02', 9], ['201917', 'product02', 17], ['201918', 'product02', 12]])

# plot
sns.lineplot(data=df, x='calendar_week', y='value', hue='product_name');

If the calendar_week values are strings, it plots the second graph after the first one.如果 calendar_week 值是字符串,它会在第一个图形之后绘制第二个图形。 If the calendar_week values are integers, it fills the data from 201852 to 201899 automatically.如果 calendar_week 值是整数,它会自动填充从 201852 到 201899 的数据。 What's the best way to plot both graphs on one sorted x-axis with only the given calendar_week values?仅使用给定的 calendar_week 值在一个排序的 x 轴上绘制两个图的最佳方法是什么?

Here is the plot with calendar_week as string:这是以 calendar_week 为字符串的图: 将 x 轴值作为字符串绘制

Here is the plot with calendar_week as int:这是带有 calendar_week 为 int 的图: 将 x 轴值作为 int 绘图

Thanks for help.感谢帮助。

It's a bit roundabout, but I think you need first to convert your week numbers into real dates, plot, then use a custom formater on the x-axis to show the week number again.这有点迂回,但我认为您首先需要将周数转换为实际日期,绘图,然后在 x 轴上使用自定义格式化程序再次显示周数。

df = pd.DataFrame(columns=['calendar_week', 'product_name', 'value'],
                  data=[['201850', 'product01', 1], ['201905', 'product01', 10], ['201910', 'product01', 7],
                       ['201840', 'product02', 4], ['201911', 'product02', 9], ['201917', 'product02', 17], ['201918', 'product02', 12]])

df['date'] = pd.to_datetime(df.calendar_week+'0', format='%Y%W%w')
# plot
fig, ax = plt.subplots()
sns.lineplot(data=df, x='date', y='value', hue='product_name', ax=ax)
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%Y-%W"))
fig.autofmt_xdate()

在此处输入图片说明

I'm from Germany and I have to deal with ISO weeks, so I ended up doing this:我来自德国,我必须处理 ISO 周,所以我最终这样做了:

import pandas as pd
import seaborn as sns
import datetime
import matplotlib
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

# data
df = pd.DataFrame(columns=['calendar_week', 'product_name', 'value'],
                  data=[['201850', 'product01', 1], ['201905', 'product01',     10], ['201910', 'product01', 7],
                       ['201840', 'product02', 4], ['201911', 'product02', 9], ['201917', 'product02', 17], ['201918', 'product02', 12]])

# convert calendar weeks to date
df['date'] = df['calendar_week'].apply(lambda x: datetime.datetime.strptime(x + '-1', '%G%V-%u'))

# plot
fig, ax = plt.subplots()
sns.lineplot(data=df, x='date', y='value', hue='product_name', ax=ax)
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%G%V'))
fig.autofmt_xdate()
plt.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM