Mar-27-2020, 07:22 AM
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
So I tried below by adding scale variable in scatter plot- but it throws error
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
Output: price rating obs
0 3 0 4
1 2 0 1
2 1 0 4
3 3 1 8
4 2 1 21
5 1 1 20
6 3 2 26
7 2 2 22
8 1 2 23
9 3 3 15
10 2 3 12
11 1 3 9
12 3 4 7
13 2 4 4
14 1 4 4
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()