简体   繁体   English

如何使用 for 循环绘制 plot 散点图

[英]How to use a for loop to plot scatter plots

I have a target variable y_data and predictor variables saved in X_data.我有一个目标变量 y_data 和预测变量保存在 X_data 中。 Since I know all my X variables are numeric, I am trying to plot scatter plots of target variable against each X variable.因为我知道我所有的 X 变量都是数字,所以我正在尝试 plot 目标变量针对每个 X 变量的散点图。 My python codes are included here.我的 python 代码包含在这里。 However, my code will overlay all the scatter plots in 1 graph.但是,我的代码将覆盖 1 个图中的所有散点图。 How can I have them individually plotted and Xlabel individually added to each plot?我怎样才能将它们单独绘制并将 Xlabel 单独添加到每个 plot? Any help is appreciated!任何帮助表示赞赏!

f = plt.figure(figsize=(6,6))
ax = plt.axes()

for column in X_data:
    ax.plot(y_data, X_data[column],
   marker = 'o', ls='', ms = 3.0)

You need to include the creation of a figure inside the loop.您需要在循环内创建图形。 Create a list of figures, then append to the list each individual figure:创建一个图形列表,然后将 append 添加到列表中的每个图形:

figures = []

for column in X_data:
    f = plt.figure(figsize=(6,6))
    figures += [f]  # This appends a figure f to the list of figures
    ax = plt.axes()
    ax.plot(y_data, X_data[column], marker = 'o', ls='', ms = 3.0)

This should do it, but I haven't tested the code.这应该可以,但我还没有测试代码。

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

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