Not able to crack a simple visualization – missing something basic – plz guide - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Not able to crack a simple visualization – missing something basic – plz guide (/thread-25335.html) |
Not able to crack a simple visualization – missing something basic – plz guide - darpInd - Mar-27-2020 Hello Readers, I have been trying to get a simple visualization but not able to crack and getting demotivated. Please help! Objective:- I want to create a simple scatter plot which have marker / dot size as per the size of observation (‘obs’ column) Dataframe name = df1 I have tried following:import matplotlib.pyplot as plt import pandas as pd plt.scatter(df1.price,df1.rating) plt.show()of course, it does provides scatter plot, but doesn't provide size of dots. So I tried below by adding scale variable in scatter plot- but it throws error import matplotlib.pyplot as plt import pandas as pd plt.scatter(df1.price,df1.rating, s = df1.obs) plt.show()Then I tried following to build my understanding. N = 45 x, y = np.random.rand(2, N) c = np.random.randint(1, 5, size=N) s = np.random.randint(10, 220, size=N) fig, ax = plt.subplots() scatter = ax.scatter(x, y, c=c, s=s) plt.show()It gives nice plot -- and I tried to replicate this one for my dataframe like below but getting error again :( x = df1['price'] y = df1['rating'] s = df1['obs'] fig, ax = plt.subplots() scatter = ax.scatter(x, y, s=s) plt.show()Then I thought that may be I have to convert dataframe column into numpy array first and then scatter() will recognise it, so I did below, again FAILED!! please help me crack it and provide some guidance on what I am missing.. must be some basic as I am new import numpy as np import matplotlib.pyplot as plt x=df1['price'].to_numpy() y = df1['rating'].to_numpy() scale=df1['obs'].to_numpy() fig,ax = plt.subplots() scatter = ax.scatter (x,y,s=scale) plt.show() RE: Not able to crack a simple visualization – missing something basic – plz guide - buran - Mar-27-2020 (Mar-27-2020, 07:22 AM)darpInd Wrote: So I tried below by adding scale variable in scatter plot- but it throws errorwhat error does it show - post the full traceback. It should work import pandas import matplotlib.pyplot as plt df = pandas.DataFrame({'price':[3, 2, 1, 3, 2, 1, 3, 2, 1, 3, 2, 1, 3, 2, 1,], 'rating':[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4], 'obs':[4, 1, 4, 8, 21, 20, 26, 22, 23, 15, 12, 9, 7, 4, 4]}) plt.scatter(df.price, df.rating, s=df.obs*10) # I scale df.obs by factor of 10 just to see the difference in size better plt.show()[attachment=805] RE: Not able to crack a simple visualization – missing something basic – plz guide - darpInd - Mar-27-2020 Here is the error.. btw I tried by doing df1.obs*10.. it is the same error which I got without using *10:
(Mar-27-2020, 07:59 AM)buran Wrote:(Mar-27-2020, 07:22 AM)darpInd Wrote: So I tried below by adding scale variable in scatter plot- but it throws errorwhat error does it show - post the full traceback. It should work RE: Not able to crack a simple visualization – missing something basic – plz guide - buran - Mar-27-2020 As the error indicates you have strings in your dataframe. Probably you read from file, without converting the type? You need to convert to number (at least the column used to calcuate the size) RE: Not able to crack a simple visualization – missing something basic – plz guide - darpInd - Mar-27-2020 (Mar-27-2020, 08:18 AM)buran Wrote: As the error indicates you have strings in your dataframe. Probably you read from file, without converting the type? Thanks you buran!! That was indeed the case, i converted to numeric and got it resolved!! Somehow , I assumed that my dataframe is having only int, hence the toruble. Good learning though! |