在matplotlib和seaborn之间共享x轴 [英] Share x axis between matplotlib and seaborn

查看:292
本文介绍了在matplotlib和seaborn之间共享x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在熊猫DataFrame中有数据:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

np.random.seed(786)

df = pd.DataFrame({'a':np.arange(0, 1, 0.05),
                   'b':np.random.rand(20) - .5})

print (df)
       a         b
0   0.00  0.256682
1   0.05 -0.192555
2   0.10  0.393919
3   0.15 -0.113310
4   0.20  0.373855
5   0.25 -0.423764
6   0.30 -0.123428
7   0.35 -0.173446
8   0.40  0.440818
9   0.45 -0.016878
10  0.50  0.055467
11  0.55 -0.165294
12  0.60 -0.216684
13  0.65  0.011099
14  0.70  0.059425
15  0.75  0.145865
16  0.80 -0.019171
17  0.85  0.116984
18  0.90 -0.051583
19  0.95 -0.096527

我想绘制barplot并添加垂直线:

I would like plot barplot and add vertical line:

plt.figure(figsize=(10,5))
sns.barplot(x = 'a', y = 'b', data = df)
plt.vlines(x = 0.45, ymin = 0, ymax = 0.6, color = 'red', linewidth=5)

壁虱标签存在问题,因为重叠线和线也应在点0.45上插入到0的位置,对于x axis.

There are problems with ticklabels, because overlaping and also line should be in point 0.45 instaed near 0 for x axis.

我尝试了 link1 中的许多解决方案, link2 link3 link4 ,但仍然无法正确设置两个图的轴.

I try many solutions from link1, link2, link3, link4 but still problem set correctly axis for both plots.

什么问题?可以在图之间共享x轴吗?

What is problem? Is possible share x axis between plots?

预期的输出-垂直线正确对齐,并且x轴上的刻度线不重叠:

Expected output - correctly aligned vertical line and also not overlaping ticks in x axis:

推荐答案

小节中的x轴是分类的,因此它没有df.a的值作为真实比例,而仅作为刻度标签.您可以更改例如df.a[19] = 2,除了最后一个刻度线的标签,其他都不会改变.

The x-axis in the barplot is categorical, so it doesn't have the values of your df.a as a real scale, but only as tick labels. You could change e.g. df.a[19] = 2 and nothing will change except the label of the last bar tick.

因此,分类轴表示第一个小节的坐标为0,第二个小节为1,依此类推……最后一个为19.

So categorical axis means the coordinates are 0 for the first bar, 1 for the second and so on ... 19 for the last.

然后我的方法是将垂直线设置为xpos * 19/.95:

My approach then would be to set the vertical line at xpos * 19/.95:

plt.vlines(x = .45*19/.95, ymin = 0, ymax = 0.6, color = 'red', linewidth=5)

对于一般情况,您可以添加一个lambda函数来计算转化:

For the general case you could add a lambda function to calculate the conversion:

f = lambda x: (x-df.a.values[0]) * (df.a.size-1) / (df.a.values[-1] - df.a.values[0])
plt.vlines(x = f(.45), ymin = 0, ymax = 0.6, color = 'red', linewidth=5)

但是,由于df.a.values仅作为刻度标签打印,因此应该从头到尾呈线性排列.

However, as df.a.values is only printed as tick labels, it should go linearly from start to end.

关于x轴标签的问题:我只能说它没有出现在我的系统上,上述图的代码与您的图相同,除了垂直线.也许是在一次接一个vlines尝试时引入的.

Regarding the problem with x-axis labeling: I just can tell that it doesn't appear at my system, the code for the plot abovevis identical to yours, except the vertical line. Perhaps it was introduced while doing one attempt of vlines after another.

这篇关于在matplotlib和seaborn之间共享x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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