___  ___    _ _    _  _ _____   _____
 / __|/ _ \  | | |  | || |_ _\ \ / / __|
| (_ | (_) | |_  _| | __ || | \ V /| _|
 \___|\___/    |_|  |_||_|___| \_/ |___|

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

Data Visualization using Python's Matplotlib Library

BY: @rohancmr | CREATED: Jan. 23, 2018, 11:57 a.m. | VOTES: 6 | PAYOUT: $25.75 | [ VOTE ]

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516549765/scafbhifl0win5wtcqtz.png]

Matplotlib is an open source Python plotting library. It is written by John D.Hunter and has an active development community.
Matplotlib produces publication quality figures in Python scripts, IPython shell, Jupyter notebook, web applications and several gui toolkits. Using this we can generate variety of charts like plots, histograms, barcharts, scatter plots, error plots etc.
One of the most popular used case of Matplotlib is for data analysis and visulation.

In this tutorial, I'll show how we can use Matplotlib for creating line charts, bar charts, pie charts and donut charts.

What this tutorial covers ?

Difficulty

Installation

Matplotlib requires pyparsing, cycler, numpy and pytz libraries for installation. Matplotlib and all of its dependencies can be installed using pip.

pip install matplotlib

Verify if it is properly installed by importing matplotlib.

import matplotlib

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516549094/tv25qbujyhbsyuasbsm4.gif]

Creating a Line Charts

The below three line of code generates a simple line chart.

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516549119/ish5vr0z5uqflbcriout.png]

import matplotlib.pyplot as plt

The plt.plot(X, Y) plots the data to a canvas in the computer's memory.

In the above example:
X = [10, 20, 30]
Y = [2, 4, 6]

plt.plot([10, 20, 30], [2, 4, 6])

plt.show()

Output

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516549193/owbqfy0khpd9uctqlayw.png]

The chart window has several options :

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516549307/vqlrre2pfhxtgqtlu9si.gif]

Adding Chart Title and labels

plt.title("This is example-2")

plt.xlabel("X-values")

plt.ylabel("Y-values")

Code with X and Y labels and chart title

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516549410/vjdm74aqaqsanagy1z0h.png]

Output

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516549420/bf9k8r2lq3y0c0junwps.png]

Set Limits on X and Y axis

Use axis() function to set limit on x and y axis . The syntax is plt.axis(xmin, xmax, ymin, ymax).

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516551029/wl1cemtziqlti3bgb0nn.png]

In above code we have set xmin = 10, xmax = 40, ymin = 5, ymax = 15

Output :

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516551058/hbbomsurwx3b405rrsih.png]

Notice since the max limit on x axis is set to 40, the value for x=50 is not plotted.

Changing line color and type

The plot() function accepts a third argument which represents the color and type. By default, it is represented by a solid blue line. The syntax is mentioned below :

plt.plot([X-values], [Y-values], 'b-'

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516693532/cvjjwociadxexoo0i2yv.png]

In the above code :
'go' represents a green dotted
'rs' represents a red square
'b^' represents a blue triange
'y--' represents a yellow dashes

Output:

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516694239/u4m6umaxtfzkq6s7xk2f.png]

Creating Bar Charts

Bar charts are created using bar() function.

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516696212/ncplctlpoe48if8o7hoh.png]

In the above code, the plt.bar(x, y, alpha=0.8) creates a bar chart based on values of x and y.
alpha=0.8 represents the opacity of the bar. The value of alpha ranges from 0 to 1.

Output
[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516696492/gmbj5utvsiv4vupk97t2.png]

The barh() function creates a horizontal bar chart.

barh(x, y, alpha=0.8)

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516696617/kt21hnp1yw6fsf6zke9w.png]

Output

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516696636/dl4wkz3ubkkyeri1nzux.png]

Creating Pie Charts

We can create pie chart using pie() function.

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516701384/dlcxt0hgowjdsed9xc2n.png]

The above code, creates a pie chart based on the value of each items.
The area of whole chart represents 100% of data. The parts of the pie chart are called wedges. The area of wedges represents the percentage of each item with respect to the whole data.
By default the pie() function arranges the wedges in counter clockwise direction.

autopct parameter controls how the percentages are displayed in the wedges.

By default, the pie chart is in oval shape. To make it circular, we use plt.axis('equal').

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516702126/ovzedautp551bqypotgn.png]

Creating Donut Charts

To create a donut chart, we first create a pie chart and impose a white circle on top of it which makes it look like a donut.

The below code creates a white circle.

my_circle = plt.Circle((0, 0), 0.7, color='white')
p = plt.gcf()
p.gca().add_artist(my_circle)

Full code to create a donut.

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516703400/evmsitknqx5wupusklht.png]

Output

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516703501/goi0ry2ckzuoa9p8rlf8.png]

I have covered few basic charts that can be created using Matplotlib. There are several advance charts that can be created with this awesome tool which are used for data analysis and machine learning.I'll cover few of them in my next Matplotlib tutorial.

Posted on Utopian.io - Rewarding Open Source Contributors

TAGS: [ #utopian-io ] [ #matplotlib ] [ #python ] [ #charts ] [ #data-visualization ]

Replies

@manishmike10 | Jan. 24, 2018, 3:10 p.m. | Votes: 1 | [ VOTE ]

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

@rohancmr | Jan. 24, 2018, 3:17 p.m. | Votes: 0 | [ VOTE ]

Thank you.

@rohancmr | Jan. 24, 2018, 3:16 p.m. | Votes: 0 | [ VOTE ]

@originalworks

@utopian-io | Jan. 25, 2018, 3:21 a.m. | Votes: 0 | [ VOTE ]

Hey @rohancmr I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
- Vote for my Witness With SteemConnect
- Proxy vote to Utopian Witness with SteemConnect
- Or vote/proxy on Steemit Witnesses

[IMAGE: https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif]

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

@steemitboard | Jan. 3, 2019, 10:46 a.m. | Votes: 0 | [ VOTE ]

Congratulations @rohancmr! You received a personal award!

https://steemitimages.com/70x70/http://steemitboard.com/@rohancmr/birthday1.png1 Year on Steemit

Click here to view your Board

> Support SteemitBoard's project! Vote for its witness and get one more award!

@steemitboard | Jan. 3, 2020, 10:32 a.m. | Votes: 0 | [ VOTE ]

Congratulations @rohancmr! You received a personal award!

https://steemitimages.com/70x70/http://steemitboard.com/@rohancmr/birthday2.pngHappy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!
[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>