HomeToolsData 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 look. With Plotly, a robust Python library, we’ll find out about the different sorts of plots and how you can unlock the potential of our data and transform it into charming visual narratives.

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

Before proceeding with any data, importing all of the needed libraries and reading the information is crucial. Here’s how we are able to do it:

pip install plotly

Bar Plots

Bar plots offer a concise technique 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 easy and accessible.

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

  • First, it imports the needed libraries, including Plotly Express and NumPy. Then, it generates random data for the x and y axes using NumPy’s randint function.
  • In this instance, 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 have a look at how you can customize the bar charts. The bar mode could be tailored to suit specific preferences by utilizing keyword arguments.  Let’s explore the instance provided below:

Here, the bar is adjusted based on the colour 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 flexible method for scrutinizing data distribution and discerning relationships amongst data variables. They visually represent trends inside datasets, presenting data points along the x and y axes.  Plotting scatter plots using Plotly is easy, allowing for effortless exploration of information 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 scale of every data point, while the ‘species’ column dictates each color and symbol, ensuring distinct representations for every 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 comparable to time series evaluation, 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 selected data category. It’s commonly used for example 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 about to “sepal_width,” indicating that the width of the pie slices shall be determined by the sepal width column of the dataset.
  • The ‘names’ parameter specifies that the slices shall be labeled in accordance with 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 visible 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 evaluation.

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

  • Initially, it imports the needed 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 right 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 about to ‘rainbow’ to define the colour 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 generally known as a ternary graph or simplex plot, depicts three variables that sum to a continuing inside an equilateral triangle. These plots offer a particular strategy to analyze data, especially in fields comparable to chemistry, geology, and engineering, where proportions are crucial. With Plotly’s flexibility, users can generate informative ternary plots to disclose patterns and relationships of their data.

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

  • It first imports the needed 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 every data point.
  • Additional styling options are provided inside the marker dictionary, setting the colour to red, size to 14, and defining a line width 2. Finally, the fig.show() function is known as 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 flexible 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, amongst others. Renowned for its interactivity, Plotly enables creating engaging, dynamic graphs that could be seamlessly embedded into web sites.

References

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Must Read