The technique of using visual representations of our data to spot trends and linkages is known as data visualization. To visualize data, we can employ a number of Python data visualization frameworks, like Matplotlib, Seaborn, Plotly, etc. Python is equally renowned for its interactive charts. In this tutorial, we’ll learn how we can plot interactive graphs in Python. Additionally, Jypyter notebook will be used for coding and visualization, so it is advised that you install it on your PC.
You may also like
3-D Interactive Plots using Python
Importance of Data visualization
The use of multiple graphs to plot data is usually beneficial. By organizing data into a more comprehensible format and highlighting trends and outliers, data visualization aids in the telling of stories. A strong visualization highlights important information while reducing data noise. For instance, it is exceedingly challenging to gain insights by simply looking at the numbers and data in tabular form. However, it is simple to understand various trends in the dataset when the data is plotted using various graphs.
The following are some of the benefits of data visualizations.
- Analyzing the Data in a Better Way: Business stakeholders can concentrate on the areas that need attention by analyzing reports. Visual mediums assist analysts in comprehending the crucial information required for their line of work. Whether it’s a sales report or a marketing strategy, a visual representation of the data helps businesses make better analyses and decisions that enhance revenues.
- Faster Decision Making: People process images more quickly than they do long, laborious tabular forms or reports. Decision-makers can move fast based on fresh data insights if the data is well-communicated, accelerating both decision-making and corporate growth.
- Making Sense of Complicated Data: Business users can utilize data visualization to understand their massive data sets. They gain by being able to spot fresh patterns and data mistakes. The users can focus on locations that show red flags or progress by making sense of these patterns. This procedure then propels the company forward.
Python modules for data visualizations
Here is a list of Python modules that can be used to visualize data in various forms.
- Matplotlib: Matplotlib is a Python plotting library that allows you to construct static, dynamic, and interactive visualizations. NumPy is its computational mathematics extension. Despite the fact that it is over a decade old, it is still the most popular plotting library in the Python world.
- Seaborn: A Python module called Seaborn is used to make statistical visuals. It features sophisticated tools for producing statistical visualizations that are both aesthetically pleasing and instructive. Data scientists primarily utilize Seaborn for publishing and practical demonstrations.
- Pandas: Pandas can also be used to visualize data.
- ggplot: Python ggplot is a plotting library that is based on the ggplot2 library for R programming. The letter gg stands for Grammar of Graphics in ggplot, and creating graphs with it is related to writing sentences with proper grammar.
- Bokeh: Bokeh is a Python library for creating interactive visualizations for modern web browsers. It helps you build beautiful graphics, ranging from simple plots to complex dashboards with streaming datasets.
- Pygal: Pygal is a Python module that creates SVG (Scalable Vector Graphics) graphs/charts in a variety of styles. Pygal is highly customizable, yet also extremely simplistic, which is a very rare combination.
- Plotly: Plotly is a visualization library that is used to create data visualizations for free.
- Geoplotlib: Geoplotlib is a powerful API that can be used for various types of map representations, such as Voronoi tesselation, Delaunay triangulation, markers, and so on.
- Folium: Folium is a powerful Python library that helps you create several types of Leaflet maps. By default, Folium creates a map in a separate HTML file.
- Gleam: Gleam programs are made up of bundles of functions and types called modules. Each module has its own namespace and can export types and values to be used by other modules in the program.
- Altair: Altair is a declarative statistical visualization library for Python, based on Vega and Vega-Lite.
Interactive graphs in Python
Before going to plot different interactive graphs in Python, make sure that you have installed the Plotly module on your system. You can use the pip command to install the Plotly module.
#installing plotly module pip install plotly
Once the installation is complete, you can check the version of the installed module by running the following command.
# importing the plotly module import plotly # printing the version of plolty print(plotly.__version__)
As you can see, in my case, I have installed Plolty 5.10.0 on my system.
Interactive line plots in Python
A line chart also known as a line graph or curve chart is a type of chart that displays information as a series of data points called ‘markers’ connected by straight line segments. It is a basic type of chart common in many fields. Mostly line charts are used to plot time series data.
First, we will create a random dataset and then will plot an interactive line chart to represent the random data using Plotly.
# importing modules import random import plotly.express as px #Generate 10 random numbers between 10 and 30 x_axis = [i for i in range (10)] y_axis = random.sample(range(10, 100), 10) # plottig the interactive graphs in Python fig = px.line(x=x_axis, y=y_axis, title='Interactive line plot') fig.show()
Each time you will run the code, you will get a different graph because the data is generated randomly. The plot is fully interactive, you can zoom in and out. See the full interactivity of the graph from here.
Another important thing about interactive line plots is that we can plot various line plots in one graph as well. This time we will load the dataset from the Plotly module and will plot various line graphs in one plot.
# loading dataset about europe data = px.data.gapminder().query("continent=='Europe'") # plotting interactive graphs in Python fig = px.line(data, x="year", y="lifeExp", color='country', title='Asian countries') fig.show()
As you can see, we were able to plot various line plots in one graph using Plotly.
Interactive scattered plots in Python
A scatter plot is a diagram where each value in the data set is represented by a dot. Scatter plots are used to observe the relationship between variables and use dots to represent the relationship between them.
Let us again create a random dataset and then we will visualize the data using an interactive scattered plot.
#Generate 10 random numbers between 50 and 100 x_axis = [i for i in range (50)] y_axis = random.sample(range(10, 100), 50) # plotting interactive graphs in Python fig = px.scatter(x=x_axis, y=y_axis) fig.show()
As you can see, the data is randomly distributed because we created a random dataset.
We can also plot the scattered plot for different classes depending on the dataset. For example, the iris data which has three different output classes. We can plot each class with different colors. Let us first load the iris dataset and then plot the graph.
# loading iris dataset data = px.data.iris() # plotting interactive graphs in Python fig = px.scatter(data, x="petal_width", y="sepal_length", color="species", size='sepal_width') fig.show()
As you can see, we have plotted the scattered plot for various output classes in different colors.
Interactive histogram plots in Python
A histogram is a plot that lets us discover, and show, the underlying frequency distribution (shape) of a set of continuous data. This allows the inspection of the data for its underlying distribution (e.g., normal distribution), outliers, skewness, etc.
Let us create a random dataset and then visualize it using an interactive histogram.
# importing random module import random # creating random dataset data = [random.randint(1, 100) for _ in range(50)] # plottig interactive graphs in Python fig = px.histogram(data) fig.show()
As you can see, we have plotted a histogram for the random data.
The histogram can also be used to plot categorical values as well. For example, the following histogram shows the total tip received over weekdays.
# loading the tips dataset df = px.data.tips() # interactive graphs in Python fig = px.histogram(df, x="day") fig.show()
As you can see that we can plot the histogram for the categorical values as well.
In some cases, we can also set the number of bins by ourselves. A histogram displays numerical data by grouping data into “bins” of equal width. Each bin is plotted as a bar whose height corresponds to how many data points are in that bin. Bins are also sometimes called “intervals”, “classes”, or “buckets”. Let us now plot an interactive histogram with 50 bins.
# loading the data data = px.data.tips() # plotting data fig = px.histogram(df, x="total_bill", nbins=50) fig.show()
As you can see, we have plotted a histogram with 50 bins.
Interactive bar plots in Python
A bar chart or bar graph is a chart or graph that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. The bars can be plotted vertically or horizontally. A vertical bar chart is sometimes called a column chart.
Let us now plot a bar chart with random data.
#Generate 10 random numbers between 5 and 30 x_axis = [i for i in range (5)] y_axis = random.sample(range(10, 30), 5) # creating bar chart fig = px.bar(x=x_axis, y=y_axis) fig.show()
The bar charts can be on the y-axis as well. Let us now use the dataset about metals to plot a stacked bar plot. A stacked chart is a form of bar chart that shows the composition and comparison of a few variables, either relative or absolute, over time. They are also called stacked bars or column charts, they look like a series of columns or bars that are stacked on top of each other. Let us plot the stack bars.
# importing dataset data = px.data.medals_long() # stacked bar charts fig = px.bar(data, x="nation", y="count", color="medal") fig.show()
The process of finding trends and correlations in our data by representing it pictorially is called Data Visualization. To perform data visualization in python, we can use various python data visualization modules such as Matplotlib, Seaborn, Plotly, etc. In this short article, we discuss how we can plot interactive graphs in Python using Python.