I want to plot a multiple variables scatter

I am trying to analyze the relationship of difference variables by ploting scatter variables against each other. But error keeps coming up. Here it said ‘ValueError: num must be 1 <= num <= 12, not 0’. I wonder anyother way to plot the scatter or how to fix the problem?

visual_data = pd.read_csv('merge_first_time.csv')
visual_data.Date = pd.to_datetime(visual_data.Date)import numpy as np
import itertools
col_name = list(visual_data.columns)
print(col_name)
visual_data.iloc[:,1]
plt.close('all')
subplot_start = 251
col_number = range(7,len(col_name))
col_pairs = list(itertools.combinations(col_number, 2))
print(col_pairs)
#plt.subplot(subplot_start)
#visual_data.iloc[:,col_pairs[1][0]]
#plt.scatter(visual_data.iloc[:,col_pairs[1][0]],visual_data.iloc[:,col_pairs[1][1]])
print(len(col_pairs))
for i in range(0,len(col_pairs)):
        plt.subplot(subplot_start)
        plt.scatter(visual_data.iloc[:,col_pairs[i][0]],visual_data.iloc[:,col_pairs[i][1]])
        plt.xlabel(col_name[col_pairs[i][0]])
        plt.ylabel(col_name[col_pairs[i][1]])

        subplot_start += 1

plt.show()


You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Hi @sakae28 - welcome to the forum.

There’s a couple of things which are preventing me from being able to replicate your issue, without which it’s tricky to help troubleshoot it.

  1. That does not appear to be the entirety of the program.
  2. I don’t know what the format of the data in merge_first_time.csv is so I can’t recreate the file to test your program.

If you can provide the entire program (so I don’t have to guess that maybe your alias plt is something pulled from matplotlib) and the dataset you’re feeding in to it, we might be able to help.

Hi there,
I am so sorry I confused you. Now I will make it more clear. The simplifying code is as following. And the data will be in the picture so that you will know what I am dealing with.

import matplotlib.pyplot as plt
import matplotlib.dates as mdates


visual_data = pd.read_csv('merge_first_time.csv')
visual_data.Date = pd.to_datetime(visual_data.Date)
visual_data.Date.head()
import numpy as np

col_name = list(visual_data.columns)
print(col_name)
plt.close('all')
plt.figure(1)
ax = 551

for i in list(range(7,12)):
    for j in list(range(7,12)):
        plt.subplot(ax)
        plt.scatter(visual_data.iloc[:,i],visual_data.iloc[:,j])
        plt.xlabel(col_name[i])
        plt.ylabel(col_name[j])
        ax += 1

the error is ‘num must be 1 <= num <= 30, not 0’ occured in ‘plt.subplot(ax)’

Thank you!

A link to the exercise would be a great place to start. Please post same in a reply.

Hi there,
I am so sorry that this is a coursework from my school. I don’t have a link to the exercise but a few documents. And I can’t upload those documents since I am new here. I submit a more clear one in my last reply to ‘thepitycoder’
Thank you for your help!

1 Like

Thanks for that, though I am assuming that you also intended to import pandas as pd since you’re using pandas.read_csv?

Your error is being caused by a combination of the following two lines in your code:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates


visual_data = pd.read_csv('merge_first_time.csv')
visual_data.Date = pd.to_datetime(visual_data.Date)
visual_data.Date.head()
import numpy as np

col_name = list(visual_data.columns)
print(col_name)
plt.close('all')
plt.figure(1)
ax = 551 # <<< here!

for i in list(range(7,12)):
    for j in list(range(7,12)):
        plt.subplot(ax)
        plt.scatter(visual_data.iloc[:,i],visual_data.iloc[:,j])
        plt.xlabel(col_name[i])
        plt.ylabel(col_name[j])
        ax += 1 # <<< and here!

Because you’re calling matplotlib.pyplot.subplot() with the 3-digit integer parameter format, you’re stuck with no more than 9 subplots.

Your ranges in the for loops are each iterating 5 times for a total of 25 plots. What’s happening is the first 9 subplots are fine, but on the 10th iteration your value of ax becomes 560. This is invalid, because you cannot have a subplot with an index of 0, hence you get the error you’re seeing.

The following fixes the immediate error, if it helps:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates # i am not sure why this is here, as you aren't using it!
import pandas as pd

visual_data = pd.read_csv('merge_first_time.csv')
visual_data.Date = pd.to_datetime(visual_data.Date)
visual_data.Date.head()
import numpy as np # also here - this imported module isn't used in this code?

col_name = list(visual_data.columns)
print(col_name)
plt.close('all')
plt.figure(1)
ax = [5,5,1] # redefine ax as a list

for i in list(range(7,12)):
    for j in list(range(7,12)):
        plt.subplot(ax[0],ax[1],ax[2]) # pass the individual arguments from the list
        plt.scatter(visual_data.iloc[:,i],visual_data.iloc[:,j])
        plt.xlabel(col_name[i])
        plt.ylabel(col_name[j])
        ax[2] += 1 # increment the subplot index parameter in a way that accommodates index >= 9

Let me know how that goes for you. :slight_smile:

1 Like

Wow,this error really drove me crazy!
Thanks to your help!! I fianlly figure it out!
Thank you

1 Like

Yeah, it took me a little while to pin it down. The error doesn’t really give you a great deal of info about where it’s coming from, which doesn’t help.

Anyway, if the few changes I made have fixed the problem then that’s good. :+1:

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.