【问题标题】:Seaborn - Timeseries line breaks when hue is addedSeaborn - 添加色调时时间序列换行
【发布时间】:2022-01-15 07:29:25
【问题描述】:

我无法使用 seaborn 绘制时间序列图。通过将hue 添加到情节中,时间序列情节会中断。我无法弄清楚为什么时间序列图会在两者之间中断以及如何阻止日期重叠。

复制问题的代码如下:

test_data = {'Date':['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04', '2021-01-06', '2021-01-08', '2021-01-09'],
             'Price':[20, 10, 30, 40, 25, 23, 56],
             'Kind': ['Pre', 'Pre', 'Pre', 'Pre', 'Current', 'Post', 'Post']}
test_df = pd.DataFrame(test_data)
test_df['Date'] = pd.to_datetime(test_df['Date'])
sns.lineplot(data=test_df, x="Date", y="Price", hue='Kind')

如何解决换行和日期重叠的问题?

【问题讨论】:

  • 你能描述一下你期望的输出吗?三个“种类”类别的日期不相交,“当前”类别只有一个点,其中线图没有意义

标签: python pandas matplotlib seaborn data-visualization


【解决方案1】:

尝试添加stylemarkers 参数来处理带有“Kind”==“Current”的孤立点:

import seaborn as sns
import matplotlib.pyplot as plt

fig = plt.figure()
sns.lineplot(data=test_df, x="Date", y="Price", style='Kind', markers=['o', 'o', 'o'], hue='Kind')
fig.autofmt_xdate()

显示情节:

【讨论】:

  • 你能告诉我如何修复x轴上的重叠日期吗?
  • 使用plt.xticks(rotation=45)(或尝试rotation=90)旋转轴标签
  • 更新:添加代码以修复每个 stackoverflow.com/questions/6705581/… 的 xaxis 标签
猜你喜欢
  • 2017-10-16
  • 1970-01-01
  • 1970-01-01
  • 2016-10-12
  • 2015-05-19
  • 2018-10-23
  • 1970-01-01
  • 2018-05-15
  • 2014-10-31
相关资源
最近更新 更多