Python Visualization
Liana Harutyunyan
Programming for Data Science
April 2, 2024
American University of Armenia
liana.harutyunyan@aua.am
1
Main graph types
• lineplot
• scatterplot
• barplot
• histogram
• boxplot
• piechart
2
Plotting libraries
• Matplotlib
import matplotlib.pyplot as plt
• Seaborn
import seaborn as sns
3
Lineplot
A Line chart is a graph that represents information as a
series of data points connected by a straight line. In line
charts, each data point or marker is plotted and connected
with a line or curve.
4
Lineplot
Read yield data.csv data.
• defines x-axis as index (1, 2, 3, ..., length):
plt.plot(data["yield apples"])
5
Lineplot
Read yield data.csv data.
• defines x-axis as index (1, 2, 3, ..., length):
plt.plot(data["yield apples"])
• give the x argument as well (not to have the indices as
x):
plt.plot(data["years"], data["yield apples"])
5
Lineplots with labels
• Add labels by xlabel and ylabel.
It is always important to write what each axis
represents and have titles as well, so that the
reader can understand without looking at code.
plt.plot(data["years"], data["yield apples"])
plt.xlabel("Years")
plt.ylabel("Yield of Apples")
plt.title("Yield of apples over the years")
plt.show()
6
Lineplot - multiple lines
• For two lines on the same graph, you just need two
plt.plots so that these are collected together and
pictured in one graph.
plt.plot(data["years"], data["yield apples"])
plt.plot(data["years"], data["yield oranges"])
• You also need to have one plt.show() after the plot
functions.
• Note that to have label for each lineplot, we need to
specify label argument for each. We also need
plt.legend() before plt.show() to show the legend.
7
Lineplot
To show each data in the lineplot, we can use different
markers.
Example: marker=’o’, marker=’x’ - highlights the points with
different markers.
• Many different marker shapes like a circle, cross,
square, etc. are provided by Matplotlib.
8
Figures
Before defining plot details, you can use the plt.figure
function to change the size of the figure.
plt.figure(figsize=(12, 6))
9
Barplot
• When you have categorical data, you can represent it
with a bar graph.
• A bar graph plots data with the help of bars, which
represent value on the y-axis and category on the x-axis.
• Bar graphs use bars with varying heights to show the
data which belongs to a specific category.
plt.bar(data["years"], data["yield apples"])
10
Plot Zoom-in
You can also zoom in to the graph using plt.xlim for
zoom-in in x-axis, and plt.ylim for zoom-in in y-axis.
Example:
plt.bar(data["years"], data["yield apples"])
plt.ylim(0.8, 1)
11
Barplot
We can also stack bars on top of each other. Let’s plot the
data for apples and oranges.
plt.bar(data["years"], data["yield apples"])
plt.bar(data["years"], data["yield oranges"],
bottom=data["yield apples"])
12
Histograms
• A Histogram is a bar representation of data that varies
over a range.
• It plots the height of the data belonging to a range along
the y-axis and the range along the x-axis.
plt.hist(data["yield apples"])
• It has bins parameter, that controls the number of bins
the data is represented.
13
Scatterplots
Scatter plots are used when we have to plot two continuous
variables present at different coordinates.
plt.scatter(data["yield apples"], data["yield oranges"])
14
Boxplots
A boxplot is a standardized way of displaying the
distribution of data, that includes quantiles, median,
minimum and maximum.
plt.boxplot(data["yield apples"])
15
Seaborn
An easy way to make your charts look beautiful is to use
some default styles from the Seaborn library.
Example - put sns.set style("darkgrid") before your
plotting code.
There are five preset seaborn themes: darkgrid, whitegrid,
dark, white, and ticks.
16
Seaborn
Seaborn is like an alternative for the matplotlob library, that
has a bit different syntax, and nicer plots (of course this is
subjective).
The syntax is also similar to R’s plotting library ggplot syntax.
17
Seaborn alternatives
The common syntax is
sns.chart name(data = data, x = ”column 1”, y = ”column 2”)
• lineplot - sns.lineplot
sns.lineplot(x = ”years”, y = ”yield apples”, data = data)
sns.lineplot(x = ”years”, y = ”yield oranges”, data = data)
18
Seaborn alternatives - Barplot
Seaborn has different datasets for examples.
Let’s load ”tips” dataset:
tips df = sns.load dataset("tips")
With similar syntax, for barplot:
sns.barplot(x=’day’, y=’total bill’, data=tips df)
The values are averaged by ”day” attribute.
Let’s do the yield data example as well.
19
Seaborn alternatives - Barplot
If you want to compare bar plots side-by-side, you can use
the hue argument. The comparison will be done based on
the third feature specified in this argument.
sns.barplot(x=’day’, y=’total bill’, hue=’sex’,
data=tips df)
What will happen if we interchange the x and y?
20
Seaborn alternatives - Histograms
Import seaborn’s iris dataset.
flowers df = sns.load dataset("iris")
sns.histplot(flowers df, x="sepal length")
Exercises:
• Add ”hue” attribute as type of flower.
• Alpha parameter gives transperancy, if we have
intersections.
21
Seaborn alternatives - Scatterplots
Similarly, we have sns.scatterplot() function.
Exercise: Plot sepal length VS sepal width scatterplot. Add
color based on the type of flower.
22
Seaborn - titles
For seaborn titles, we write:
sns.chart type(...).set(title="", xlabel="",
ylabel="")
23
Summary
Reading
https://www.simplilearn.com/tutorials/python-tutorial/data-
visualization-in-python
References
https://www.simplilearn.com/tutorials/python-tutorial/data-
visualization-in-python
Questions?
24