Sie sind auf Seite 1von 20

DATA VISUALIZATION USING

PYPLOT
Introduction
There are multiple tools for
performing visualization in data science. In
Python we can use two exclusive libraries for
visualization, commonly known as matplotlib
and seaborn.
Introduction
• Matplotlib: Python based plotting library
offers matplotlib with a complete 2D support along
with limited 3D graphic support. It is useful in
producing publication quality figures in interactive
environment across platforms. It can also be used for
animations as well.
• Seaborn: Seaborn is a library for creating informative
and attractive statistical graphics in python. This library
is based on matplotlib. Seaborn offers various features
such as built in themes, color palettes, functions and
tools to visualize univariate, bivariate, linear regression,
matrices of data, statistical time series etc which lets us
to build complex visualizations.
Setup installation steps
Open cmd and go to directory by using following
command:
C:\> cd Python27\Scripts
Then type
pip install matpoltlip
with internet connection.
Installing matplotlib
Checking install version of matplotlib
C:\Python27\Scripts>pip show matplotlib
Example1-Graph
Here's some basic code to generating one of the
most simple graphs that we can, it will take us only
3 lines.
#Importing pyplot
from matplotlib import pyplot as plt
#Plotting to our canvas
plt.plot([1,2,3],[4,5,1])
#Showing what we plotted
plt.show()
Output
Example-2
from matplotlib import pyplot as plt

x = [5,8,10]
y = [12,16,6]

plt.plot(x,y)

plt.title('Epic Info')
plt.ylabel('Y axis')
plt.xlabel('X axis')

plt.show()
Output
Line Graph
A line chart or line graph is a type of chart which
displays information as a series of data points
called ‘markers’ connected by straight line
segments.
Line graphs are usually used to find relationship
between two data sets on different axis; for
instance X, Y.
Example-Line graph:Population
dataset of India and Pakistan
from matplotlib import pyplot as plt
year = [1960, 1970, 1980, 1990, 2000, 2010]
pop_pakistan = [44.91, 58.09, 78.07, 107.7, 138.5, 170.6]
pop_india = [449.48, 553.57, 696.783, 870.133, 1000.4, 1309.1]

plt.plot(year, pop_pakistan, color='g')


plt.plot(year, pop_india, color='orange')
plt.xlabel('Countries')
plt.ylabel('Population in million')
plt.title('Pakistan India Population till 2010')
plt.show()
From the above source I got the data of both X and
Y axis. year will be on x-axis where population of
both countries on Y axis. Since I need two different
lines so plot was called twice. The color attribute
used to assign the color of the line.

xlabel and ylabel are being used to give some


friendly name to the axises and finally .title() for
giving the name of the entire graph. When runs it
will appear like given below:
Bar chart
Another very popular chart on matplotlib is the bar
chart. For the bar chart we will use the bar()
function, where we define the position of the bars
on the X axis and their height on the Y axis.
Additionally, we can also configure another
characteristics for the chart, like width of the bars,
color, among others. The X axis will be a range with
the same quantity of items as the Y axis. Let’s see a
simple example, where we will store the
configurations we want in variables and then we
will pass them to the bar() function:
Example-Bar Chart
import matplotlib.pyplot as plt;

# Variables for the bar chart


y_axis = [20,50,30]
x_axis = range(len(y_axis))
plt.bar(x_axis, y_axis, width=.5, color='orange')
plt.show()
Output
Pie chart
import matplotlib.pyplot as plt

# Data to plot
labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 210]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0, 0, 0) # explode 1st slice

# Plot
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)

plt.axis('equal')
plt.show()
Output
Upgrade Python version
pip install - - upgrade pip

Das könnte Ihnen auch gefallen