简体   繁体   English

seaborn:如何在 X 轴上添加第二级标签

[英]seaborn: How to add a second level of labels on the X axis

I need to plot a time series.我需要绘制一个时间序列。 Dates on the X axis and values on the Y axsis, but I also need to specify the day of week on the X axsis. X 轴上的日期和 Y 轴上的值,但我还需要在 X 轴上指定星期几。

ax = sns.lineplot(x='date', y='value', data=df)

I expect to be able to add day of week (another column from df) on the X axis.我希望能够在 X 轴上添加星期几(df 的另一列)。 example with Excel以 Excel 为例

You can try to do this by adding a second x-axis.您可以尝试通过添加第二个 x 轴来做到这一点。 Please find below a code you'll need to adapt to your problem.请在下面找到您需要适应您的问题的代码。 I guess there are better ways to do that but it should works.我想有更好的方法可以做到这一点,但它应该有效。

x = np.arange(1000)
x2 = np.arange(1000)*2
y = np.sin(x/100.)

fig = plt.figure()

ax = plt.subplot(111)
sns.lineplot(x, y)
plt.xlim(0, 1000)
ax.xaxis.set_major_locator(MultipleLocator(200))

ax2 = ax.twiny()
sns.lineplot(x2, y, visible=False)
plt.xlim(0, 2000)
ax2.xaxis.set_major_locator(MultipleLocator(400))
ax2.spines['top'].set_position(('axes', -0.15))
ax2.spines['top'].set_visible(False)
plt.tick_params(which='both', top=False)

在此处输入图片说明

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

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