【问题标题】:seaborn: How to add a second level of labels on the X axisseaborn:如何在 X 轴上添加第二级标签
【发布时间】:2019-10-02 20:33:20
【问题描述】:

我需要绘制一个时间序列。 X 轴上的日期和 Y 轴上的值,但我还需要在 X 轴上指定星期几。

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

我希望能够在 X 轴上添加星期几(来自 df 的另一列)。 example with Excel

【问题讨论】:

  • 一个简单的解决方案是生成一个date2 列,该列由dateday_of_week 组合而成。或者你可以试试here的解决方案。

标签: python matplotlib time-series seaborn


【解决方案1】:

您可以尝试通过添加第二个 x 轴来执行此操作。请在下面找到您需要适应您的问题的代码。我想有更好的方法可以做到这一点,但它应该有效。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 2012-05-17
    • 2015-12-19
    • 2018-12-08
    相关资源
    最近更新 更多