Home > Software engineering >  How to use a for loop to plot scatter plots
How to use a for loop to plot scatter plots

Time:10-10

I have a target variable y_data and predictor variables saved in 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. My python codes are included here. However, my code will overlay all the scatter plots in 1 graph. How can I have them individually plotted and Xlabel individually added to each 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)

CodePudding user response:

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:

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.

  • Related