Back

“A Beginner’s Guide to Data Visualization with Python”

Created 2 years ago
141 Views
0 Comments
KuganN
@KuganN
KuganN
@KuganNProfile is locked. Login

Why Visualize?

Visualize to Analyze Data

Data visualization is the process of identifying trends and correlations in the data by representing it in the form of pictures.

Most Popular Python Libraries For Data Visualization :

  • Matplotlib

  • Seaborn

  • Plotly

  • Bokeh

  • GGplot

1.Matplotlib

Matplotlib is a cross-platform, data visualization and graphical plotting library for Python and its numerical extension NumPy.

Installation:

#pip
pip install matplotlib

The most commonly used plots are

Line plot

from matplotlib import pyplot
# x-axis values
roll_num = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# y-axis values
marks = [55,75,96,75,36,45,87,99,100]

pyplot.plot(roll_num, marks)
pyplot.show()

For more information about the pyplot API and interface, refer to What Is Pyplot In Matplotlib

pyplot.plot( )function is used to plot the line representing the data.

pyplot.show( )function is used to display the plotted values .

Output:

Lineplot

2.Scatter Plot

from matplotlib import pyplot 
 
# x-axis values 
roll_num = [1, 2, 3, 4, 5, 6, 7, 8, 9] 
 
# y-axis values 
marks = [55,75,96,75,36,45,87,99,100] 
 
pyplot.scatter(roll_num, marks) 
  
pyplot.show() 

pyplot.scatter(x-axis,y-axis) is used to plot the data in a scattered fashion.

Output:

Scatterplot

3. Histogram

from matplotlib import pyplot 
  
marks = [55,75,96,75,36,45,87,99,100] 
 
pyplot.hist(marks, bins = 7) 
 
pyplot.show()

pyplot.hist( )function is used to represent the data points through a Histogram. It accepts two parameters:

  • List of the data to be plotted

  • Number of ranges(bins) to divide and display the data.

output:

Histogram

4.Bar Plot

import numpy as np
import matplotlib.pyplot
  
city = ('Coimbatore', 'Chennai', 'Trichy', 'Tanjavur', 'Selam', 'Sivagangai')
y_val = np.arange(len(city))
rank = [4, 7, 1, 3, 2, 5]
  
pyplot.bar(y_val, rank, align='center')
pyplot.xticks(y_val, city)
pyplot.ylabel('Rank')
pyplot.title('City')
  
pyplot.show()

pyplot.xticks is used to set the tick locations for x-axis.

pyplot.ylabel( )is used to set a label-text value to the data of y-axis.

pyplot.title( )sets a title value to the Bar Chart.

Output:

Barplot

5. Pie Charts

import numpy as np
import matplotlib.pyplot
  
city = ('Coimbatore', 'Chennai', 'Trichy', 'Tanjavur', 'Selam', 'Sivagangai')
 
rank = [4, 7, 1, 3, 2, 5]
explode = (0.2, 0, 0, 0, 0, 0)  
colors = ['yellowgreen', 'pink', 'purple', 'grey', 'red', 'orange']
  
 
pyplot.pie(rank, explode=explode, labels=city, colors=colors,
        autopct='%1.1f%%', shadow=True, startangle=120)
  
pyplot.axis('equal')
  
pyplot.show()

pyplot.pie( )function is used to represent the data in the form of a pie chart.

These parameters of pyplot.pie() serve the following functions:

  • explode: provides a scalar value to set a fraction of the pie chart apart.

  • labels: provides text values to represent each fraction of the chart.

  • colors: provides the colors to set to each fraction of the chart.

  • autopct: labels the wedges or the fractions of the chart with a numeric value.

  • shadow: Accepts boolean values. If set to TRUE, it creates shadow beneath the fractions of the pie chart.

startangle: rotates the start of the chart by a particular degree from x-axis.

Output:

Piechart

2.Seaborn

The Seaborn visualization library allows you to plot statistical graphics in Python in beautiful default styles and color schemes.

Different categories of plot in Seaborn

Installation

#pip
pip install seaborn

seaborn depends on

  • Python 2.7 or 3.4+

  • numpy

  • scipy

  • pandas

  • matplotlib

plotting a simple line plot

# importing packages
import matplotlib.pyplot as plt
import seaborn as sns

sns.set_style("darkgrid")

x = [1, 2, 3, 4, 5]
y = [1, 5, 4, 7, 4]

sns.lineplot(x, y)
plt.show()

Output:

set_style(style=None, rc=None)

set_style() method is used to set the aesthetic of the plot.

There are five themes available in Seaborn.

  • darkgrid

  • whitegrid

  • dark

  • white

  • ticks

For more Customizing visit website

https://www.geeksforgeeks.org/python-seaborn-tutorial/

Heat Map:

The heatmap is a way of representing the data in a 2-dimensional form. The data values are represented as colors in the graph.

Syntax:

seaborn.heatmap(data, , vmin=None, vmax=None, cmap=None, center=None, annot_kws=None, linewidths=0, linecolor=’white’, cbar=True, *kwargs)

Creating a simple Heatmap:

# importing packages
import numpy as np
import seaborn as sb
import matplotlib.pyplot as plt

data = np.random.rand(4, 6)
#That will create a 2-dimensional array with four rows and six columns
heat_map = sb.heatmap(data,cmap="YlGnBu")
plt.xlabel("Values on X axis")
plt.ylabel('Values on Y axis')

plt.show()

Output:

For more details about Heatmap visit

https://www.geeksforgeeks.org/seaborn-heatmap-a-comprehensive-guide/

I hope you enjoyed this post and learned something new and useful.

Comments
Please login to comment.