Python Forum
Not able to crack a simple visualization – missing something basic – plz guide
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not able to crack a simple visualization – missing something basic – plz guide
#1
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

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()
Reply
#2
(Mar-27-2020, 07:22 AM)darpInd Wrote: So I tried below by adding scale variable in scatter plot- but it throws error
what 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()
   
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Here is the error.. btw I tried by doing df1.obs*10.. it is the same error which I got without using *10:
Error:
AttributeError Traceback (most recent call last) <ipython-input-40-c5c9e7d1608e> in <module> 1 import matplotlib.pyplot as plt 2 import pandas as pd ----> 3 plt.scatter(df1.price,df1.rating, s = df1.obs*10) 4 plt.show() C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, plotnonfinite, data, **kwargs) 2845 verts=verts, edgecolors=edgecolors, 2846 plotnonfinite=plotnonfinite, **({"data": data} if data is not -> 2847 None else {}), **kwargs) 2848 sci(__ret) 2849 return __ret C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs) 1599 def inner(ax, *args, data=None, **kwargs): 1600 if data is None: -> 1601 return func(ax, *map(sanitize_sequence, args), **kwargs) 1602 1603 bound = new_sig.bind(ax, *args, **kwargs) C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, plotnonfinite, **kwargs) 4496 offsets=offsets, 4497 transOffset=kwargs.pop('transform', self.transData), -> 4498 alpha=alpha 4499 ) 4500 collection.set_transform(mtransforms.IdentityTransform()) C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\collections.py in __init__(self, paths, sizes, **kwargs) 883 Collection.__init__(self, **kwargs) 884 self.set_paths(paths) --> 885 self.set_sizes(sizes) 886 self.stale = True 887 C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\collections.py in set_sizes(self, sizes, dpi) 855 self._sizes = np.asarray(sizes) 856 self._transforms = np.zeros((len(self._sizes), 3, 3)) --> 857 scale = np.sqrt(self._sizes) * dpi / 72.0 * self._factor 858 self._transforms[:, 0, 0] = scale 859 self._transforms[:, 1, 1] = scale AttributeError: 'str' object has no attribute 'sqrt'
(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 error
what 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()
Reply
#4
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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?
You need to convert to number (at least the column used to calcuate the size)

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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I must be missing something simple? SLFrey 2 469 Dec-11-2023, 03:13 PM
Last Post: SLFrey
  [SOLVED] How to crack hash with hashlib Milan 0 1,325 Mar-09-2023, 08:25 PM
Last Post: Milan
  Complete Guide to Shodan Calli 4 2,202 Jul-18-2022, 04:29 PM
Last Post: Calli
  New User online Guide NAP1947 1 1,506 Sep-06-2020, 04:36 PM
Last Post: Larz60+
  "Up to but not including" (My personal guide on slicing & indexing) Drone4four 5 2,817 Nov-20-2019, 09:38 PM
Last Post: newbieAuggie2019
  Looking for a Guide Through the Labyrinth stephenmolnar 1 2,078 Jan-23-2019, 08:22 PM
Last Post: Larz60+
  [split] Python Guide / Document? kumatul 2 2,693 Jan-16-2019, 12:49 AM
Last Post: micseydel
  Python Guide / Document? Qui_Ten 1 2,778 Jan-13-2019, 07:50 AM
Last Post: Gribouillis
  How to create a graph for direction visualization Visiting 2 2,751 Sep-22-2018, 10:49 PM
Last Post: Visiting

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020