移动matplotlib轴以相互匹配 [英] Shift matplotlib axes to match eachother

查看:33
本文介绍了移动matplotlib轴以相互匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking to overlay a scatter plot with a boxplot in matplotlib. I have created the chart but the x axes do not match--leading to the scatter plot showing dots that are shifted 1 tick to the left of the x axis for the boxplot. Below is my code.

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 在 xaxis 上绘制行,而我希望沿 x 轴绘制列.

Is it possible that the error is coming from the two different methods of plotting the data? I use matplotlib to plot the scatter and pandas to plot the boxplot. Matplotlib was plotting the rows on the xaxis, whereas I wanted the columns to be plotted along the x axis.

请参见下面的代码,输出图像.

See outputted image below from above code.

推荐答案

很难在不访问数据的情况下进行调查,但是如果您仅转换散点图的 x 坐标,它应该可以工作:

Hard to investigate without having access to data, but if you just translate the x coordinates of your scatter plot, it should work:

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

这篇关于移动matplotlib轴以相互匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆