Seaborn - 添加色调时时间序列换行
Seaborn - Timeseries line breaks when hue is added
我在使用 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')
如何解决换行符和日期重叠问题?
尝试添加 style
和 markers
参数来处理孤立点 "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()
显示情节:
我在使用 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')
如何解决换行符和日期重叠问题?
尝试添加 style
和 markers
参数来处理孤立点 "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()
显示情节: