python - 类似于 R 的图形 python
问题描述
我有一张这样的表:(忽略“索引”和“D”列)
+-------+-----------------------+----------+----------+----------+
| Index | Type | Male | Female | D |
+-------+-----------------------+----------+----------+----------+
| 44 | Life struggles | 2.097324 | 3.681356 | 1.584032 |
| 2 | Writing notes | 2.677262 | 3.354730 | 0.677468 |
| 18 | Empathy | 3.528117 | 4.083051 | 0.554933 |
| 12 | Criminal damage | 2.926650 | 2.374150 | 0.552501 |
| 20 | Giving | 2.650367 | 3.196944 | 0.546577 |
| 21 | Compassion to animals | 3.666667 | 4.178268 | 0.511602 |
| 33 | Mood swings | 2.965937 | 3.451613 | 0.485676 |
| 10 | Funniness | 3.574572 | 3.104907 | 0.469665 |
| 38 | Children | 3.354523 | 3.805415 | 0.450891 |
| 47 | Small - big dogs | 3.221951 | 2.801695 | 0.420256 |
+-------+-----------------------+----------+----------+----------+
我知道如何在 R 中做到这一点,但在 python 中不知道
我试过这个:
sns.stripplot(data=df,y="Male",color="Blue")
sns.stripplot(data=df,y="Female",color="red")
但我不知道如何继续。有人有想法吗?
解决方案
这很容易用 matplotlib 完成,它只是一个以类别作为 y 值的散点图。
plt.style.use('ggplot')
fig, ax = plt.subplots()
ax.plot(df['Male'],df['Type'],'o', color='xkcd:reddish', ms=10, label='Male')
ax.plot(df['Female'],df['Type'],'o', color='xkcd:teal', ms=10, label='Female')
ax.axvline(3,ls='-',color='k')
ax.set_xlim(1,5)
ax.set_xlabel('avg response')
ax.set_ylabel('Variable')
ax.legend(bbox_to_anchor=(0.5, 1.02), loc='lower center',
ncol=2, title='group')
fig.tight_layout()
推荐阅读
- rust - 是否有可以在线程之间移动(不共享)的 Arc 类型?
- r - 如何将均值 + 标准差放入单列
- android - 引起:java.lang.NumberFormatException:对于输入字符串:模拟器上的“/storage/emulated/0/Download/**.mp4”
- docker - 重新启动停止的容器并再次执行 CMD
- rust - 包装一个异步函数
- php - 如何更改分页响应?
- google-chrome - 什么是更新 Chrome 扩展视图和资产的最佳方式和允许方式?
- html - 网页上的表单列表
- memory - 在dolphindb中查询占用内存
- oauth-2.0 - Oauth 2.0 与 Auth0 与 WSO2