python - 如何使用波浪号运算符将 R 代码重写为 Python Pandas?

标签 python pandas matplotlib seaborn

<分区>

我在 R 中有这段代码:

boxplot(ToothGrowth$len ~ ToothGrowth$supp,
    main = "Tooth growth in Guinea Pig",
    xlab = "Delivery method",
    ylab = "Tooth length")

我知道 R 中的波浪号运算符表示因变量和自变量之间的关系,换句话说: dentry 生长长度取决于供应。是否有任何可能的方法将此 R 代码“翻译”为 Python Pandas?

这是我在 R 中的数据集:

"len","supp","dose"
4.2,"VC",0.5
11.5,"VC",0.5
7.3,"VC",0.5
5.8,"VC",0.5
6.4,"VC",0.5
10,"VC",0.5
11.2,"VC",0.5
11.2,"VC",0.5
5.2,"VC",0.5
7,"VC",0.5
16.5,"VC",1
16.5,"VC",1
15.2,"VC",1
17.3,"VC",1
22.5,"VC",1
17.3,"VC",1
13.6,"VC",1
14.5,"VC",1
18.8,"VC",1
15.5,"VC",1
23.6,"VC",2
18.5,"VC",2
33.9,"VC",2
25.5,"VC",2
26.4,"VC",2
32.5,"VC",2
26.7,"VC",2
21.5,"VC",2
23.3,"VC",2
29.5,"VC",2
15.2,"OJ",0.5
21.5,"OJ",0.5
17.6,"OJ",0.5
9.7,"OJ",0.5
14.5,"OJ",0.5
10,"OJ",0.5
8.2,"OJ",0.5
9.4,"OJ",0.5
16.5,"OJ",0.5
9.7,"OJ",0.5
19.7,"OJ",1
23.3,"OJ",1
23.6,"OJ",1
26.4,"OJ",1
20,"OJ",1
25.2,"OJ",1
25.8,"OJ",1
21.2,"OJ",1
14.5,"OJ",1
27.3,"OJ",1
25.5,"OJ",2
26.4,"OJ",2
22.4,"OJ",2
24.5,"OJ",2
24.8,"OJ",2
30.9,"OJ",2
26.4,"OJ",2
27.3,"OJ",2
29.4,"OJ",2
23,"OJ",2

最佳答案

您可以为此使用 matplotlibseaborn 包:

import matplotlib.pyplot as plt
import seaborn as sns

plt.figure(figsize=(12, 9))
sns.boxplot(x = 'supp', y = 'len', data = df, palette = 'rainbow')
plt.title("Tooth growth in Guinea Pig")
plt.xlabel('Delivery method')
plt.ylabel('Tooth length')

plt.savefig('my_plot.png')

enter image description here

关于python - 如何使用波浪号运算符将 R 代码重写为 Python Pandas?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73996349/

相关文章:

python - 创建数组后,如何正确地遍历 CSV 文件中的行数组?

python - 如何在没有换行符的情况下打印numpy对象

python - 如何在 Pandas Data Frame 中创建条件列,其中列值基于其他列

python - 使用python将 'repetitive' 2D数组转换为矩阵

python - 切割轴的边界

python - numpy 中的屏蔽数组运算

Python 和 Pandas - 按日期排序

python - 如何设置 seaborn 点图的 x 轴范围?

python - 如何绘制我的数据框(python 和 pandas)

python - 动态更新 ModelForm 的 Meta 类