Python Charts — Bubble, 3D Charts with Properties of Chart

Rinu Gour
6 min readMar 18, 2019

--

Python Charts — Bubble, 3D Charts with Properties of Chart

Prerequisites for Python Charts

For this Python Chart tutorial, we will import three libraries- matplotlib, numpy, and pandas.
You can install these Python Libraries using the following commands-

  1. pip install matplotlib
  2. pip install numpy
  3. pip install pandas

You can import these as-

  1. >>> from matplotlib import pyplot as plt
  2. >>> import numpy as np
  3. >>> import pandas as pd

Do you know about Python Modules vs Packages

Python Bubble Charts

First, let’s talk about bubble charts in Python. Such a chart is a scatter plot with an extra dimension, which makes it apparently 3-dimensional. This means larger bubbles denotes higher values. Let’s build a Python bubble plot.

Python Charts — Python Bubble Charts
  1. >>> x=np.random.rand(30)
  2. >>> y=np.random.rand(30)
  3. >>> z=np.random.rand(50)
  4. >>> plt.scatter(x,y,s=z*777) #s is for global size

<matplotlib.collections.PathCollection object at 0x0863FAD0>

  1. >>> plt.show()
Python Charts — Python Bubble Charts

a. Setting bubble color

Using the color parameter, you can assign a color to your bubbles chart.

  1. >>> plt.scatter(x,y,s=z*777,c=’Chartreuse’)

<matplotlib.collections.PathCollection object at 0x0A258DB0>

  1. >>> plt.show()
Python Charts — Setting bubble color

You can even assign random colors to different bubbles chart in Python.

  1. >>> colors=np.random.rand(30)
  2. >>> plt.scatter(x,y,s=z*777,c=colors,alpha=0.5) #alpha for transparency

<matplotlib.collections.PathCollection object at 0x0A295EF0>
Read about Python Datetime Module with Quick Examples

  1. >>> plt.show()
Python Charts — Setting bubble color

b. Setting bubble shape

You can also choose what shape to apply to your bubbles chart.

  1. >>> plt.scatter(x,y,s=z*3001,marker=’D’)

<matplotlib.collections.PathCollection object at 0x0A2D0E70>

  1. >>> plt.show()
Python Charts — Setting bubble shape
  1. >>> plt.scatter(x,y,s=z*3001,marker=’<’,color=’brown’)

<matplotlib.collections.PathCollection object at 0x09E15A50>

  1. >>> plt.show()
Setting bubble shape
  1. >>> plt.scatter(x,y,s=z*3001,marker=’*’,color=’pink’)

<matplotlib.collections.PathCollection object at 0x09E508F0>
Have a Look at Python Forensics — Hash Function, Virtualization & much more

  1. >>> plt.show()
Setting bubble shape
  1. >>> plt.scatter(x,y,s=z*3001,marker=’8',color=’lavender’)

<matplotlib.collections.PathCollection object at 0x08B9AC90>

  1. >>> plt.show()
Setting bubble shape

c. Setting the edges for your Python bubbles charts

You can also set edges to your Python bubbles chart.

  1. >>> plt.scatter(x,y,s=z*4000,c=”beige”,alpha=0.4,linewidth=7)

<matplotlib.collections.PathCollection object at 0x094035B0>
Do you know how to Implement Python Switch Case Statement

  1. >>> plt.show()
Setting the edges for your bubbles charts
  1. >>> plt.scatter(x,y,s=z*4000,c=”beige”,linewidth=7,edgecolors=’brown’)

<matplotlib.collections.PathCollection object at 0x0A1EADD0>

  1. >>> plt.show()
Setting the edges for your bubbles charts

d. Mapping a color to a plot

Let’s try mapping greys to it.

  1. >>>plt.scatter(x,y,s=z*2000,c=x,cmap=”Greys”,alpha=0.4,edgecolors=”grey”,linewidth=2)

<matplotlib.collections.PathCollection object at 0x08AB91F0>

  1. >>> plt.show()
Mapping a color to a plot

Python 3D Charts

It is possible to render your Python charts in three dimensions. Here’s how we do that with Python 3D charts.
Let’s Learn Python Database Access — Python 3 MySQL
a. Python 3D Scatterplots
Here, we use the mplot3D toolkit from Python Matplotlib.

  1. >>> from mpl_toolkits.mplot3d import Axes3D
  2. >>> df=pd.DataFrame({‘X’:range(1,111),’Y’:np.random.randn(110)*13+range(1,111), ‘Z’:(np.random.randn(110)*13+range(1,111))*2})
  3. >>> fig=plt.figure()
  4. >>> ax=fig.add_subplot(121,projection=’3d’)
  5. >>> ax.scatter(df[‘X’],df[‘Y’],df[‘Z’],c=’pink’,s=60)

<mpl_toolkits.mplot3d.art3d.Path3DCollection object at 0x0A126CF0>

  1. >>> ax.view_init(27,200)
  2. >>> plt.show()
Python 3D Scatterplots

Python Charts Properties

Let’s demonstrate these online Python graphs.

Python Bubble Charts
  1. >>> x=np.arange(0,7)
  2. >>> from scipy import sin
  3. >>> y=sin(x)
  4. >>> plt.plot(x,y)

[<matplotlib.lines.Line2D object at 0x0BEE0150>]

  1. >>> plt.show()
Properties of Charts in Python

a. Titling the graph and labeling the axes

Using the title(), xlabel(), and ylabel() methods, we can do the needful.

  1. >>> plt.title(‘Sine Wave’)

Text(0.5,1,’Sine Wave’)

  1. >>> plt.xlabel(‘x’)

Text(0.5,0,’x’)

  1. >>> plt.ylabel(‘y’)

Text(0,0.5,’y’)

  1. >>> plt.plot(x,y)

[<matplotlib.lines.Line2D object at 0x043BDC70>]

  1. >>> plt.show()
Titling the graph and labeling the axes

Let’s Discuss Python Data File Formats — How to Read CSV, JSON, and XLS Files

b. Setting line color and type

To set the line color, you use a Unicode character in the parameter list-

  1. >>> plt.plot(x,y,’g’)

[<matplotlib.lines.Line2D object at 0x045E6FF0>]

Setting line color and type

Now to set the line type, we can-

  1. >>> plt.plot(x,y,’*’)

[<matplotlib.lines.Line2D object at 0x0A0A4350>]

  1. >>> plt.show()
Setting line color and type

Now, what if we wanted to set both at once?

  1. >>> plt.plot(x,y,’*g’)

[<matplotlib.lines.Line2D object at 0x0A10B810>]
Follow this link to know about Python Data Cleansing by Pandas & Numpy

  1. >>> plt.show()

Charts in Python- Setting line color and type

‘g*’ gives us the same output.

6. Styling your Python Chart

Apart from fiddling with the properties of your charts in Python, you can also style it in a few different ways. Let’s see how.

Styling your Python Chart

a. Adding Annotations

It is possible to drop in a label in your charts in Python wherever you want.

  1. >>> x=np.arange(0,7)
  2. >>> y=sin(x)
  3. >>> plt.plot(x,y)

[<matplotlib.lines.Line2D object at 0x076DCB90>]

  1. >>> plt.annotate(xy=[3.5,0],s=’The curve’) #You can add the annotations before plotting if you want

Text(3.5,0,’The curve’)

  1. >>> plt.show()
Styling your Chart- Adding Annotations

Let’s Read about Python File I/O — Python Write to File and Read File

b. Adding Legends

  1. >>> x=np.arange(0,7)
  2. >>> y=sin(x)
  3. >>> z=np.cos(x)
  4. >>> plt.plot(x,y)

[<matplotlib.lines.Line2D object at 0x07D24530>]

  1. >>> plt.plot(x,z)

[<matplotlib.lines.Line2D object at 0x07D248F0>]

  1. >>> plt.legend([‘Sine’,’Cosine’],loc=3)

<matplotlib.legend.Legend object at 0x07D249D0>

  1. >>> plt.show()
Styling your Chart — Adding Legends

How to Save Python Charts File?

Finally, let’s see what you can do when you want to save a Python chart you’ve just created.

  1. >>> plt.scatter(x,y,s=z*3001,marker=’*’,color=’pink’)

<matplotlib.collections.PathCollection object at 0x07D402F0>

  1. >>> plt.savefig(‘pinkstars.pdf’,format=’pdf’)
Python Charts File

How to Save Python Charts File Now we want to save this as a png. There is no need to make another call to plot(); simply save it.
Do you know about Python Multiple Inheritance — Python MRO

Saving Python Charts File

So, this was all about Python Charts Tutorial. Hope you like our explanation.

Conclusion

Hence, with this, we sum up our Python Charts tutorial on bubble charts and 3D charts in Python. Now you also know how to style Charts in Python, to make them aesthetically better and also aid understanding. Are there any more topics you would like us to write on? Let us know in the comments below.

--

--

Rinu Gour

Data Science Enthusiast | Research writer | Blogger | Entrepreneur