Search code examples
pythonmatplotlibgraphscatter-plot

Python Scatter Graph


Below is my code.

import matplotlib.pyplot as graph

data = [147,96,106,81,48,37,15,25,41,99,160,145]

months = ["1/22", "2/22", "3/22", "4/22", "5/22", "6/22", "7/22", "8/22", "9/22", "10/22","11/22", "12/22"]

data2 = [48,50,54,59,66,71,77,78,71,61,52,47]

graph.title("Seattle (Rain Fall vs Temperature)", fontsize = 14)

graph.xlabel("Months", fontsize = 14)

graph.ylabel("Weather Level", fontsize = 14)

graph.tick_params(axis='both',labelsize = 9)

graph.scatter(data, months, color="blue", label = "Average Rain Fall")

graph.scatter(data2, months, color="red", label = "Average Temperature")

graph.legend()

graph.scatter(data, data2, months)

graph.show()

Below is the error I am getting??? What am I doing wrong??

runfile('C:/Users/dddst279/My Files/Google Drive/My Drive/Scatter Graph.py', wdir='C:/Users/dddst279/My Files/Google Drive/My Drive')
Traceback (most recent call last):

  File "C:\Users\dddst279\My Files\Google Drive\My Drive\Scatter Graph.py", line 14, in <module>
    graph.scatter(data, data2, months)

  File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 2819, in scatter
    __ret = gca().scatter(

  File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\__init__.py", line 1412, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 4371, in scatter
    raise ValueError(

ValueError: s must be a scalar, or float array-like with the same size as x and y

Solution

  • it is clear from the error that the third argument in graph.scatter(data, data2, months) should be a scalar or a float. The months variable should be an array of scalars and not strings.

    import matplotlib.pyplot as graph
    
    data = [147,96,106,81,48,37,15,25,41,99,160,145]
    months = [1/22, 2/22, 3/22, 4/22, 5/22, 6/22, 7/22, 8/22, 9/22, 10/22,11/22, 12/22]
    data2 = [48,50,54,59,66,71,77,78,71,61,52,47]
    graph.title("Seattle (Rain Fall vs Temperature)", fontsize = 14)
    graph.xlabel("Months", fontsize = 14)
    graph.ylabel("Weather Level", fontsize = 14)
    graph.tick_params(axis='both',labelsize = 9)
    graph.scatter(data, months, color="blue", label = "Average Rain Fall")
    graph.scatter(data2, months, color="red", label = "Average Temperature")
    graph.legend()
    graph.scatter(data, data2, months)
    graph.show()
    

    This is the output you should get:

    enter image description here