如何在python中为多个散点图制作一个循环? [英] How to make a loop for multiple scatterplots in python?

查看:64
本文介绍了如何在python中为多个散点图制作一个循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动化大型数据帧矩阵的绘图过程.目标是用另一列绘制每一列.每列代表一个变量.另见下图.

I am trying to automate the plotting procedure of a large dataframe matrix. The goal is to plot each column with an other column. Each column represents a variable. See also the image below.

F.e:性别 vs 年龄、性别 vs BMI、性别 vs 吸烟、性别 vs 类型等.

F.e: sex vs age, sex vs BMI, sex vs smoke, sex vs type and so on.

为了清楚起见,我将问题简化为下图:在此处输入图片描述

For the sake of clearity, I have simplified the problem to image below: enter image description here

最初,我尝试手动绘制每个组合.但这是一个相当耗时的练习,而不是我想要的.

Initially, I tried to plot each combination by hand. But this is rather a time-consuming excersize and not what I want.

我也试过这个(不工作):

I tried also this (not working):

variables = ["Sex", "Age", "BMI"]
for variable in variables:
plt.scatter(df.variable, df.variable)
plt.xlabel('variable')
plt.ylabel('variable')
plt.title('variable vs. variable')
plt.show()

欢迎任何帮助!

PS:如果将线性回归纳入变量组合也是一个简单的练习,那也将不胜感激.

PS: If it would be a simple excersize to incorporate a linear regression on the combination of variables as well, that would also be appreciated.

您好,

娜迪亚

推荐答案

您编码的内容将每一列与自身进行了对比.你所描述的是一个嵌套循环.一个简单的升级是

What you coded plots each column against itself. What you described is a nested loop. A simple upgrade is

col_choice = ["Sex", "Age", "BMI"]

for pos, axis1 in enumerate(col_choice):   # Pick a first col
    for axis2 in enumerate(col_choice[pos+1:]):   # Pick a later col
        plt.scatter(df.loc[:, axis1], df.loc[:, axis2])

认为这会生成一个scatter可接受的序列.

I think this generates a series acceptable to scatter.

这有帮助吗?如果您想更Pythonic",请查看 itertools.product 以生成您的列选择.

Does that help? If you want to be more "Pythonic", then look into itertools.product to generate your column choices.

这篇关于如何在python中为多个散点图制作一个循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆