[英]How to change x-axis labels in Boxplots?
I want to get a picture like this (just focus on the boxplots):我想得到这样的图片(只关注箱线图):
As you can see the x-labels of the picture represent the average spacing of the boxplot.
如您所见,图片的 x 标签表示箱线图的平均间距。 I'm trying to make something similar using seaborn, matplotlib, pd.boxplot but I couldn't get it.
我正在尝试使用 seaborn、matplotlib、pd.boxplot 制作类似的东西,但我无法得到它。 The code I used is this
我使用的代码是这样的
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
%matplotlib inline
x=np.array([ 0. , 43.9 , 0. , 21.43, 0. , 46.3 , 0. , 0. ,
12.2 , 0. , 54.88, 55.56, 53.44, 55.46, 0. , 95.58,
91.03, 40.63, 65.22, 100. , 12.95, 97.89, 86.67, 100. ,
96.03, 94.12, 97.93, 100. , 95.8 ])
y=np.array([ 19.23, 54.29, 29.23, 23.33, 150. , 63.53, 35. , 12.5 ,
117.14, 66.67, 273.33, 180. , 163.75, 56.67, 230. , 141.25,
207.14, 118.75, 690. , 60. , 69.5 , 710. , 83.33, 760. ,
503.33, 218.57, 290. , 158.33, 238.33])
z=np.array([ 82.66, 82.66, 82.66, 82.66, 82.66, 82.66, 120. , 120. ,
120. , 120. , 120. , 120. , 120. , 120. , 120. , 150.76,
150.76, 150.76, 150.76, 150.76, 150.76, 150.76, 400.93, 400.93,
400.93, 400.93, 400.93, 400.93, 400.93])
df=pd.DataFrame({'RQD (%)':x,
'Spacing (mm)':y,
'Avarage Spacing':z
})
sns.boxplot(x='Avarage Spacing',y='RQD (%)',data=df,fliersize=False)
The resulting picture of the lines above is this:上面几行的结果图片是这样的:
If you look carefully at the last picture you will realize that its x-labels don't represent themselves because they represent 1, 2, 3 y 4. I say this because if x-labels would represent themself so the boxplots would be more separated如果你仔细看最后一张图片,你会发现它的 x 标签并不代表它们自己,因为它们代表 1、2、3 y 4。我这样说是因为如果 x 标签代表它们自己,那么箱线图就会更加分离
In that way I want you to help me in this problem.这样,我希望你能帮助我解决这个问题。 I want x-labels in the last picture to represent themselves in the x-axis like in the first picture.
我希望最后一张图片中的 x 标签像第一张图片一样在 x 轴上代表自己。 I want to achieve that because I want to draw another function to get something similar to the first picture.
我想实现这一点,因为我想再画一个 function 来获得与第一张图片类似的东西。
For seaborn it is not really possible but you can find a workaround below.对于 seaborn 这实际上是不可能的,但您可以在下面找到解决方法。 See also more details here: Matplotlib boxplot x axis
另请参阅此处的更多详细信息: Matplotlib boxplot x axis
For Matplotlib use positions
(see also below) and for log scale see the link posted by @JohanC in the comments ( Matplotlib boxplot width in log scale ).对于 Matplotlib 使用
positions
(另见下文)和对数刻度,请参阅@JohanC 在评论中发布的链接( Matplotlib 对数刻度中的箱线图宽度)。
For matplotlib you need to restructure the data a bit which I included in the code example below.对于 matplotlib,您需要对我在下面的代码示例中包含的数据进行一些重组。
from matplotlib import pyplot as plt
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
%matplotlib inline
x=np.array([ 0. , 43.9 , 0. , 21.43, 0. , 46.3 , 0. , 0. ,
12.2 , 0. , 54.88, 55.56, 53.44, 55.46, 0. , 95.58,
91.03, 40.63, 65.22, 100. , 12.95, 97.89, 86.67, 100. ,
96.03, 94.12, 97.93, 100. , 95.8 ])
y=np.array([ 19.23, 54.29, 29.23, 23.33, 150. , 63.53, 35. , 12.5 ,
117.14, 66.67, 273.33, 180. , 163.75, 56.67, 230. , 141.25,
207.14, 118.75, 690. , 60. , 69.5 , 710. , 83.33, 760. ,
503.33, 218.57, 290. , 158.33, 238.33])
z=np.array([ 82.66, 82.66, 82.66, 82.66, 82.66, 82.66, 120. , 120. ,
120. , 120. , 120. , 120. , 120. , 120. , 120. , 150.76,
150.76, 150.76, 150.76, 150.76, 150.76, 150.76, 400.93, 400.93,
400.93, 400.93, 400.93, 400.93, 400.93])
df=pd.DataFrame({'RQD (%)':x,
'Spacing (mm)':y,
'Avarage Spacing':z
})
# reorganize data so they fit into the matplotlib way
data = df.groupby('Avarage Spacing').agg({'RQD (%)':lambda x: list(x)}).transpose().values[0]
# use positions for the x position
# adjust width to match the size of boxplots to your preferences
plt.boxplot(data,
positions=df['Avarage Spacing'].unique(),
widths=20)
plt.show()
# in seaborn its a bit more difficult as x positons are not supported
# you could convert the categories to int and then add a tick for each step
# your categories are then plotted at the right positions as all the other
# categories are empty and plotted as blank spaces
df['Avarage Spacing'] = df['Avarage Spacing'].astype(int)
sns.boxplot(x='Avarage Spacing',y='RQD (%)',data=df,
fliersize=False,
order=range(df['Avarage Spacing'].min()-10,df['Avarage Spacing'].max()+10))
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.