This article is participating in Python Theme Month. See the link to the event for more details

After introducing Bokeh in the last article, let’s move on to the fourth library in our list. This is the last library in our list, and you might be wondering why Plotly is used. Here are the advantages

  • Potly has a hover tool that allows us to detect any outliers or outliers among our many data points.
  • It allows for more customization.
  • It makes the graphics visually more attractive.

The installation

To install it, type the following command into the terminal.

pip install plotly
Copy the code

A scatter diagram

Plotly can be used as scatter() Plotly. Express. As with Seaborn, an additional data parameter is required here.

Example:

import plotly.express as px
import pandas as pd

# Read database
data = pd.read_csv("tips.csv")

# Draw a scatter plot
fig = px.scatter(data, x="day", y="tip", color='sex')

# according to the plot
fig.show()
Copy the code

Output:

The line chart

The line chart in Plotly looks intuitive and is an excellent amalgam of Plotly, which manages various types of data and assembles easy-to-style statistics. Use px. Line to represent each data position as a vertex

Example:

import plotly.express as px
import pandas as pd

# Read database
data = pd.read_csv("tips.csv")

# Draw a scatter plot
fig = px.line(data, y='tip', color='sex')

# according to the plot
fig.show()
Copy the code

The bar chart

Bar charts in Plotly can be created using the bar() method of the Plotly. Express class.

Example:

import plotly.express as px
import pandas as pd

# Read database
data = pd.read_csv("tips.csv")

# Draw a scatter plot
fig = px.bar(data, x='day', y='tip', color='sex')

# Show plot
fig.show()
Copy the code

Output:

histogram

In plotly, histograms can be created using the histogram() function of the plotly. Express class.

Example:

import plotly.express as px
import pandas as pd

# Read database
data = pd.read_csv("tips.csv")

# Draw a scatter plot
fig = px.histogram(data, x='total_bill', color='sex')

# according to the plot
fig.show()
Copy the code

Output:

Add the interaction

Just like Bokeh, Plotly offers a variety of interactions. Let’s discuss a few of them.

Create a drop-down menu: The drop-down menu is a part of the menu button that is always displayed on the screen. Each menu button is associated with a menu widget that displays the options for that menu button when it is clicked. In Plotly, there are four possible ways to modify a chart using the updatemenu method.

  • Restyle: Modify data or data properties
  • Relayout: Modifies layout properties
  • Update: Modify data and layout properties
  • Animate: Start or pause animation

Example:

import plotly.graph_objects as px
import pandas as pd

# Read database
data = pd.read_csv("tips.csv")


plot = px.Figure(data=[px.Scatter(
	x=data['day'],
	y=data['tip'],
	mode='markers',)])# Add the drop-down menu
plot.update_layout(
	updatemenus=[
		dict(
			buttons=list([
				dict(
					args=["type"."scatter"],
					label="Scatter Plot",
					method="restyle"
				),
				dict(
					args=["type"."bar"],
					label="Bar Chart",
					method="restyle"
				)
			]),
			direction="down",
		),
	]
)

plot.show()
Copy the code

Output:

Add buttons: In Plotly, action custom buttons are used to quickly create actions directly from records. Custom buttons can be added to the page layout in CRM, marketing, and custom applications. There are four possible ways to apply custom buttons:

  • Restyle: Modify data or data properties
  • Relayout: Modifies layout properties
  • Update: Modify data and layout properties
  • Animate: Start or pause animation

Example:

import plotly.graph_objects as px
import pandas as pd

# Read database
data = pd.read_csv("tips.csv")


plot = px.Figure(data=[px.Scatter(
	x=data['day'],
	y=data['tip'],
	mode='markers',)])# Add the drop-down menu
plot.update_layout(
	updatemenus=[
		dict(
			type="buttons",
			direction="left",
			buttons=list([
				dict(
					args=["type"."scatter"],
					label="Scatter Plot",
					method="restyle"
				),
				dict(
					args=["type"."bar"],
					label="Bar Chart",
					method="restyle"
				)
			]),
		),
	]
)

plot.show()
Copy the code

Output:

Create sliders and selectors:

In Plotly, the scope slider is an input control of a custom scope type. It allows you to select a value or a range of values between the minimum and maximum ranges specified. A range selector is a tool for selecting ranges to display in a chart. It provides buttons for selecting a preconfigured range in the chart. It also provides input boxes for manually entering minimum and maximum dates

Example:

import plotly.graph_objects as px
import pandas as pd

# Read database
data = pd.read_csv("tips.csv")

plot = px.Figure(data=[px.Scatter(
	y=data['tip'],
	mode='lines',)
])

plot.update_layout(
	xaxis=dict(
		rangeselector=dict(
			buttons=list([
				dict(count=1,
					step="day",
					stepmode="backward"),
			])
		),
		rangeslider=dict(
			visible=True
		),
	)
)

plot.show()
Copy the code

Output:

summary

In this tutorial series, we drew the TIPS dataset using four different drawing modules in Python: Matplotlib, Seaborn, Bokeh, and Plotly. Each module shows the plot in its own unique way, and each module has its own set of functions. For example, Matplotlib offers more flexibility at the expense of writing more code, while Seaborn offers as a high-level language that allows people to pass a small amount of code. Each module can be used according to the task we want to accomplish.

๐Ÿฅ‡ Python for data visualization series summary

  • Matplotlib for data visualization in Python
  • Seaborn for data visualization in Python
  • Bokeh for data visualization in Python
  • Plotly for data visualization using Python

More on this at ๐Ÿงต

  • 30 Python tutorials and tips
  • Python statements, expressions, and indentation
  • Python keywords, identifiers, and variables
  • How do I write comments and multi-line comments in Python
  • Learn about Python number and type conversions through examples
  • Python data types — from basic to advanced
  • Teach you how to make a snake game in Python
  • Object-oriented programming in Python – objects, objects, and members

๐Ÿฐ Past excellent articles recommended:

  • 20 Python Tricks Everyone must Know
  • 100 Basic Python Interview Questions Part 1 (1-20)
  • 100 Basic Python Interview Questions Part 2 (21-40)
  • 100 Basic Python Interview Questions Part 3 (41-60)
  • 100 Basic Python Interview Questions Part 4 (61-80)
  • 100 Basic Python Interview Questions Part 5 (81-100)

If you do learn something new from this post, like it, bookmark it and share it with your friends. ๐Ÿค— Finally, don’t forget โค or ๐Ÿ“‘ for support