【问题标题】:Shift matplotlib axes to match eachother移动 matplotlib 轴以相互匹配
【发布时间】:2020-07-16 14:08:19
【问题描述】:

我希望在 matplotlib 中用箱线图覆盖散点图。我已经创建了图表,但 x 轴不匹配 - 导致散点图显示的点在箱线图的 x 轴左侧移动了 1 个刻度。下面是我的代码。

fig, ax = plt.subplots()

ax.scatter(traits_5, data_df[traits_5].loc[y])
ax = greats_df[traits_5].boxplot ( showfliers=False , column=traits_5)

plt.ylabel ( 'Percentile Score' )
plt.title ( "Distribution of The Greats' Scores" )

ax.yaxis.set_major_formatter(mtick.PercentFormatter(1))

plt.show ()

错误是否可能来自绘制数据的两种不同方法?我使用 matplotlib 绘制散点图,使用 pandas 绘制箱线图。 Matplotlib 在 x 轴上绘制行,而我希望沿 x 轴绘制列。

从上面的代码看下面的输出图像。

【问题讨论】:

  • 请提供完整的 MCVE。通过查看代码,我不知道您的数据是什么。您可以使用np.random.randn 或类似方法来生成列。
  • 很高兴看到 PercentFormatter 被使用 :)

标签: python matplotlib


【解决方案1】:

如果无法访问数据,很难进行调查,但如果您只是转换散点图的 x 坐标,它应该可以工作:

ax.scatter([x+1 for x in traits_5], data_df[traits_5].loc[y])

【讨论】:

    猜你喜欢
    • 2012-09-01
    • 2017-03-25
    • 1970-01-01
    • 2016-09-11
    • 2020-11-09
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    相关资源
    最近更新 更多