Data Visualization with Python Plotly : Creating Stunning Charts and Graphs

Data Visualization with Python Plotly : Creating Stunning Charts and Graphs

Data visualization is like storytelling with visuals, making complex information easy to interpret at a glance. With Plotly, a powerful Python library, we'll learn about the different types of plots and how to unlock the potential of our data and transform it into captivating visual narratives.

This tutorial'll explore how interactive visualization can enhance our understanding of data and increase engagement. From sales forecasts to stock analysis, interactive plots created with Plotly can support various applications.

Before proceeding with any data, importing all the necessary libraries and reading the data is essential. Here's how we can do it:

pip install plotly

Bar Plots

Bar plots offer a concise means of comparing categorical data. They present it through rectangular bars whose heights vary based on the values being compared. Constructing bar charts in a visualization tool like Plotly is straightforward and accessible.

This Python code snippet demonstrates how to create a bar plot using Plotly Express.

  • First, it imports the necessary libraries, including Plotly Express and NumPy. Then, it generates random data for the x and y axes using NumPy's randint function.
  • In this example, 100 random integers between 1 and 100 are generated for the x and y coordinates. Next, it creates a bar plot using Plotly.
  • Express's px.bar function, specifying the random_x values for the x-axis and the random_y values for the y-axis. Finally, it displays the plot using the fig.show() method.
import plotly.express as px
import numpy
 
# creating random data through randomint 
# function of numpy.random 
np.random.seed(42) 
   
random_x= np.random.randint(1, 101, 100) 
random_y= np.random.randint(1, 101, 100)
 
fig = px.bar(random_x, y = random_y)
fig.show()

Output:

Now let's see how to customize the bar charts. The bar mode can be tailored to suit specific preferences by utilizing keyword arguments.  Let’s explore the example provided below:

Here, the bar is adjusted based on the color attributes.

import plotly.express as px
 
df = px.data.iris()
 
fig = px.bar(df, x="sepal_width", y="sepal_length", color="species")
fig.show()

Output:

Scatter Plots

Scatter plots offer a versatile method for scrutinizing data distribution and discerning relationships among data variables. They visually represent trends within datasets, presenting data points along the x and y axes.  Plotting scatter plots using Plotly is straightforward, allowing for effortless exploration of data patterns and correlations.

This Python code snippet utilizes Plotly Express to create an interactive scatter plot visualizing the iris dataset.

  • The dataset is first loaded using the px.data.iris() function. The scatter plot is then generated with 'sepal_width' on the x-axis and 'sepal_length' on the y-axis.
  • Each data point is differentiated by species through color, symbol, and size parameters, enhancing visual clarity and facilitating species comparison.
  • The 'petal_length' column determines the size of each data point, while the 'species' column dictates both color and symbol, ensuring distinct representations for each species.
  • Additionally, hovering over data points triggers the display of 'petal_width' values, enriching the user experience with additional insights.
import plotly.express as px
df = px.data.iris()
 
fig = px.scatter(df, x="sepal_width", y="sepal_length",
                 color="species", size='petal_length',
                 symbol="species",  # This parameter changes the symbols based on species
                 hover_data=['petal_width'])
fig.show()

Output:

Line Plots

Line plots are effective tools for visualizing continuous data, making them suitable for various applications such as time series analysis, mathematical functions, and other continuous datasets. They offer insights into data trends, highlighting maximum and minimum points along a continuous scale. Line plots are particularly useful for depicting time series data, including stock prices, sales figures over time, and similar chronological data.

This code snippet utilizes Plotly Express, a high-level interface for creating visualizations in Plotly, to generate a line plot using the iris dataset.

  • First, the iris dataset is loaded using px.data.iris().
  • Then, the px.line() function creates a line plot, with 'sepal_width' data mapped to the x-axis and 'sepal_length' data mapped to the y-axis.
  • Finally, fig.show() displays the resulting plot.
import plotly.express as px
 
# Loading the iris dataset
df = px.data.iris()
 
fig = px.line(df, x="sepal_width", y="sepal_length")
fig.show()

Output:

Pie Plots

A pie plot visually represents numerical proportions by dividing a circle into sectors, each corresponding to a specific data category. It's commonly used to illustrate percentages and aids in conveying data distribution effectively through distinct portions and color coding.

This Python code utilizes the Plotly Express library to generate a pie chart visualizing the Iris dataset.

  • After loading the dataset using the px.data.iris() function, the code creates a pie chart using the px.pie() method.
  • The 'values' parameter is set to "sepal_width," indicating that the width of the pie slices will be determined by the sepal width column of the dataset.
  • The 'names' parameter specifies that the slices will be labeled according to the species column.
  • Additionally, the 'title' parameter provides a title for the chart, while 'hover_data' is used to display additional information upon hovering over each slice, showing the corresponding sepal length. Finally, the fig.show() function displays the generated pie chart.
import plotly.express as px
 
# Loading the iris dataset
df = px.data.iris()
 
fig = px.pie(df, values="sepal_width", names="species",
             title='Iris Dataset', hover_data=['sepal_length'])
fig.show()

Output:

Contour Plots

A contour plot shows curves where a function maintains constant values in a two-variable space. These curves, called contour lines, connect points with equal function values, offering a visual representation of the function's behavior. It uses a 2D numerical array 'z' to generate interpolated lines representing isovalues, highlighting regions with similar function values for analysis.

This Python code utilizes Plotly to generate a 2D contour plot.

  • Initially, it imports the necessary Plotly library. Then, it defines arrays for the x and y features using NumPy's arange function, creating evenly spaced values.
  • These features are arranged into a 2D grid using NumPy's meshgrid function. Next, the Z values are calculated based on the cosine of X divided by 2 plus the sine of Y divided by 4.
  • Finally, a contour plot is created using Plotly's Contour function, specifying the x and y features and the calculated Z values. The 'colorscale' parameter is set to 'rainbow' to define the color scheme of the contour plot.
import plotly.graph_objects as go

feature_x = np.arange(0, 50, 2) 
feature_y = np.arange(0, 50, 3) 

# Creating 2-D grid of features 
[X, Y] = np.meshgrid(feature_x, feature_y) 

Z = np.cos(X / 2) + np.sin(Y / 4) 

fig = go.Figure(data =
	go.Contour(x = feature_x, y = feature_y, z = Z,
			colorscale='rainbow'
			))

fig.show()

Output:

Ternary Plots

A ternary plot, also known as a ternary graph or simplex plot, depicts three variables that sum to a constant within an equilateral triangle. These plots offer a distinctive way to analyze data, especially in fields such as chemistry, geology, and engineering, where proportions are crucial. With Plotly's flexibility, users can generate informative ternary plots to reveal patterns and relationships in their data.

This Python code utilizes Plotly, a powerful visualization library, to create a ternary scatter plot based on the iris dataset.

  • It first imports the necessary modules, including Plotly's graph_objects and express. The iris dataset is loaded using Plotly's built-in iris data.
  • The code then initializes a Figure object with a Scatterternary trace, specifying the mode as 'markers' for individual data points.
  • The 'a', 'b', and 'c' attributes represent the sepal length, sepal width, and petal length, respectively, for each data point.
  • Additional styling options are provided within the marker dictionary, setting the color to red, size to 14, and defining a line width 2. Finally, the fig.show() function is called to display the resulting ternary scatter plot.
import plotly.express as px 
import plotly.graph_objects as go 


df = px.data.iris() 
fig = go.Figure(go.Scatterternary({ 
	'mode': 'markers', 
	'a': df['sepal_length'], 
	'b': df['sepal_width'], 
	'c': df['petal_length'], 
	'marker': { 
		'color': 'red', 
		'size': 14, 
		'line': {'width': 2} 
	} 
})) 

fig.show() 

Output:

Conclusion
Plotly stands out as a versatile open-source Python module designed for data visualization, offering support for an array of graphs including line charts, scatter plots, bar charts, histograms, and area plots, among others. Renowned for its interactivity, Plotly enables creating engaging, dynamic graphs that can be seamlessly embedded into websites.



References

About the author
Manya Goyal

AI Developer Tools Club

Explore the ultimate AI Developer Tools and Reviews platform, your one-stop destination for in-depth insights and evaluations of the latest AI tools and software.

AI Developer Tools Club

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to AI Developer Tools Club.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.