Data Visualization with
Python
Python provides various libraries that come with different features
for visualizing data. All these libraries come with different features
and can support various types of graphs. In this tutorial, we will be
discussing four such libraries.
• Matplotlib
• Seaborn
• Bokeh
• Plotly
• A bar plot (or bar chart) is a graphical representation that uses
rectangular bars to compare different categories.
• The height or length of each bar corresponds to the value it
represents.
• The x-axis typically shows the categories being compared, while
the y-axis shows the values associated with those categories.
• This visual format makes it easy to compare quantities across
different groups.
This function takes several parameters:
• x: The categories (e.g., fruits).
• height: The corresponding values (e.g., sales).
• width: The width of the bars (default is 0.8).
• bottom: The baseline for the bars (default is 0).
• align: How to align bars ('center' or 'edge')
Customizing Bar Colors
• You can customize the color of the bars by using the color
parameter in the bar() function:
Creating Horizontal Bar Plots
For horizontal bar plots, you can use the barh() function. This
function works similarly to bar(), but it displays bars horizontally:
Adjusting Bar Width
You can control the width of the bars using the width parameter:
Multiple bar plots
• Multiple bar plots are used when comparison among the data set is
to be done when one variable is changing.
• We can easily convert it as a stacked area bar chart, where each
subgroup is displayed by one on top of the others.
• It can be plotted by varying the thickness and position of the bars.
• Following bar plot shows the number of students passed in the
engineering branch:
Length from origin
Stacked bar plot
• Stacked bar plots represent different groups on top of one another.
• The height of the bar depends on the resulting height of the
combination of the results of the groups.
• It goes from the bottom to the value instead of going from zero to
value.
• The following bar plot represents the contribution of boys and girls
in the team.
represent the x-axis positions for each team
Line chart in Matplotlib - Python
• A line chart or line plot is a graphical representation used to show
the relationship between two continuous variables by connecting
data points with a straight line. It is commonly used to visualize
trends, patterns or changes over time.
• In Matplotlib line charts are created using the pyplot sublibrary
which provides simple and flexible functions for plotting data.
• In a line chart, the x-axis typically represents the independent
variable while the y-axis represents the dependent variable.
where:
• xx: The values for the x-axis
• yy: The corresponding values for the y-axis
• color: Specifies the color of the line
• linestyle: Defines the style of the line
• linewidth: Controls the thickness of the line
• marker: Adds markers at data points
Consider a simple example where we visualise the weekly
temperature using a line chart in Matplotlib
Matplotlib Simple Line Plot
• For creating a basic line chart, you can use the plot() function.
• This function draws a line by connecting data points on the x-axis
and y-axis, making it easy to visualize relationships between two
continuous variables.
Adding Labels and Title to a Line Plot
• To improve readability, you can use the xlabel(), ylabel() and title()
functions.
• These functions help in clearly identifying the axes and the purpose
of the line chart.
Using Markers in Line Plots
• Markers help highlight individual data points on a line plot making
it easier to identify exact values.
• They are especially useful for small datasets or precise
comparisons. The marker parameter in [Link]() specifies the shape
of symbols used to mark each data point.
Adding Grid to a Line Chart
• Grids make it easier to read values and follow trends across the
chart. You can enable grids using the grid() function
Line Chart with Annotations
• For adding annotations to a line chart you can use the annotate()
function.
• This function allows you to display additional information such as
the exact x and y values directly on the data points, improving
clarity and data interpretation.
Multiple Line Charts Using Matplotlib
• To create multiple line charts in separate containers you can use
the figure() function.
• Each call to figure() generates a new plotting area, making it easier
to visualize and compare different datasets independently.
Multiple Plots on the Same Axis
• To plot multiple lines on the same axis, you can call the plot()
function multiple times before displaying the graph.
• This approach is useful for comparing different datasets within the
same coordinate system.
Fill the Area Between Two Lines
• To fill the region between two line plots, you can use the
fill_between() function.
• This function shades the area between two curves, helping
visualize the difference or range between datasets.
Saving a Line Chart to a File
Instead of displaying a plot you can save it as an image using the
savefig() function.
Plotting Trigonometric Functions
• Trigonometric functions such as sine, cosine and tangent can be
visualized using [Link]() by generating input values over a
continuous range.
• The [Link]() function creates evenly spaced values,
allowing smooth and accurate curves.