[英]How to add vertical grid lines in a grouped boxplot in Seaborn?
I want to create a grouped boxplot
with vertical grid lines in seaborn
, ie, at each tick, there should be a vertical line, just as in a regular scatter plot. 我想创建一个在seaborn
具有垂直网格线的分组boxplot
,即,在每个刻度线处,应该有一条垂直线,就像在常规散点图中一样。
Some example code: 一些示例代码:
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import numpy.random as rnd
some_x=[1,2,3,7,9,10,11,12,15,18]
data_for_each_x=[]
for i in range(0, len(some_x)):
rand_int=rnd.randint(10,30)
data_for_each_x.append([np.random.randn(rand_int)])
sns.set()
sns.boxplot(data=data_for_each_x, showfliers=False)
plt.show()
How it looks: 看起来如何:
If I understood you correctly, you want the vertical white grid lines instead of the horizontal lines which you are getting currently. 如果我理解正确,你需要垂直的白色网格线而不是你当前获得的水平线。 This is one way to do so: 这是一种方法:
Create an axis object ax
and then assign this to the sns.boxplot
. 创建一个轴对象ax
,然后将其分配给sns.boxplot
。 Then you can choose which grid lines to show by using a boolean argument to ax.xaxis.grid
and ax.yaxis.grid
. 然后,您可以使用ax.xaxis.grid
和ax.yaxis.grid
的布尔参数来选择要显示的网格线。 Since you want the vertical grid lines, turn off the y-grid ( False
) and turn on the x-grid ( True
). 由于您需要垂直网格线,请关闭y网格( False
)并打开x网格( True
)。
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import numpy.random as rnd
fig, ax = plt.subplots() # define the axis object here
some_x=[1,2,3,7,9,10,11,12,15,18]
data_for_each_x=[]
for i in range(0, len(some_x)):
rand_int=rnd.randint(10,30)
data_for_each_x.append([np.random.randn(rand_int)])
sns.set()
sns.boxplot(data=data_for_each_x, showfliers=False, ax=ax) # pass the ax object here
ax.yaxis.grid(False) # Hide the horizontal gridlines
ax.xaxis.grid(True) # Show the vertical gridlines
In case you want to show both x and y grids, use ax.grid(True)
如果要显示x和y网格,请使用ax.grid(True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.