[英]Matplotlib/Seaborn plot a boxplot with on the x-axis different range of values categories
我想在 x 軸上制作一個箱線圖,其中 x 變量在不同的范圍內分割,例如:0-5、5-10、10+。 有沒有辦法在 Matplotlib/Seaborn 中有效地做到這一點,而不必基於子集創建不均勻的新列? 因此,對於下面的這個示例數據集,我想要一個箱線圖,其中包含 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
提前致謝。
使用 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.