简体   繁体   English

Matplotlib/Seaborn 在 x 轴上绘制一个箱线图,其中包含不同范围的值类别

[英]Matplotlib/Seaborn plot a boxplot with on the x-axis different range of values categories

I want to make a boxplot with on the the x-axis having the x variable split in different ranges, for eg: 0-5, 5-10, 10+.我想在 x 轴上制作一个箱线图,其中 x 变量在不同的范围内分割,例如:0-5、5-10、10+。 Is there a way to do this efficiently in Matplotlib/Seaborn without having to create uneven new columns based on subsetting?有没有办法在 Matplotlib/Seaborn 中有效地做到这一点,而不必基于子集创建不均匀的新列? So for this example dataset below a I want a boxplot with 3 boxes 0-5 (1a4j,1a6u,1ahc), 5-10 (1brq,1bya), 10+ (1bya,1bbs) given the rot_bonds variable因此,对于下面的这个示例数据集,我想要一个箱线图,其中包含 3 个框 0-5 (1a4j,1a6u,1ahc), 5-10 (1brq,1bya), 10+ (1bya,1bbs) 给定rot_bonds变量

    structure rot_bonds no_atoms logP
0   1a4j    3   37  2.46
1   1a6u    4   17  1.58
2   1ahc    0   10  -0.06
3   1bbs    20  51  4.81
4   1brq    5   21  5.51
5   1bya    10  45  -9.75

Thanks in advance.提前致谢。

With seaborn you could use the slicing into ranges as the x axis, and for example 'no_atoms' as the y-values for the boxplot:使用 seaborn,您可以使用切片范围作为x轴,例如'no_atoms'作为箱线图的 y 值:

from matplotlib import pyplot as plt
from io import StringIO
import pandas as pd
import seaborn as sns

s = '''    structure rot_bonds no_atoms logP
0   1a4j    3   37  2.46
1   1a6u    4   17  1.58
2   1ahc    0   10  -0.06
3   1bbs    20  51  4.81
4   1brq    5   21  5.51
5   1bya    10  45  -9.75'''

df = pd.read_csv(StringIO(s), delim_whitespace=True)
sns.boxplot(x=pd.cut(df['rot_bonds'], [0, 5, 10, 1000]), y='no_atoms', data=df)
plt.show()

示例图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM