No Result
View All Result
DevRescue
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
DevRescue
Home Blog Python

Plotly Bar Chart in Python with Code

by Khaleel O.
July 29, 2021
in Python
Reading Time: 4 mins read
A A
plotly bar chart python
Plotly Bar Chart in Python

Ploty Bar Chart in Python can be created using Plotly Express. Plotly Express is officially described as “a terse, consistent, high-level API for creating figures”. First, let’s install the library if you don’t already have it:

pip install plotly-express

Now, let’s create a simple bar chart with the following code:

import plotly.express as px
import pandas as pd
  
d = {'CLASS_TYPE': ['A','B','C','D','E'], 'STUDENTS': [120,225,30,109,308]}

df = pd.DataFrame(data=d)

fig = px.bar(df, x='CLASS_TYPE', y='STUDENTS')
fig.show()

Let’s explain what is going on here:

  1. We import the plotly.express library because is the recommended starting point for creating most common figures. The plotly.express module contains functions that can create entire plots at once.
  2. We import pandas because our dataset is a dataframe.
  3. d is our dataset. We have 2 columns and 5 rows. One column is our CLASS_TYPE and includes 5 classes A, B, C, D and E. The other column STUDENTS are the amounts of students that belong to each class. From this data we create our dataframe df.
  4. px.bar() accepts our dataframe, the CLASS_TYPE on the x-axis and the STUDENTS count on the y-axis. I returns an object of type plotly.graph_objects.Figure which we assign to fig.
  5. fig.show() shows our simple bar graph/bar plot in a new browser window in the default browser. It has a small menu bar on the upper right as well as tooltips when you mouse over the bars.

When we execute our code we get the following output:

Simple Bar Plot with Plotly Express
Simple Bar Plot with Plotly Express A

We can flip the axis such that CLASS_TYPE is on the vertical axis and STUDENTS is on the horizontal axis:

fig = px.bar(df, x='STUDENTS', y='CLASS_TYPE')
fig.show()

And now we have the following plot:

Simple Bar Plot with Plotly Express
Simple Bar Plot with Plotly Express B

We can add different colors to the face of each bar with the following code:

fig = px.bar(df, x='STUDENTS', y='CLASS_TYPE', color='CLASS_TYPE', title='Students by Class')
fig.show()

Adding the color parameter and the title parameter gives us the following graph:

Simple Bar Plot with Plotly Express
Simple Bar Plot with Plotly Express C

Notice that we added a title to our chart along with colors to easily differentiate the bars. Recall that bar plots are best at allowing us to compare categories of data.

Finally we can add data labels with the following code:


fig = px.bar(df, x='STUDENTS', y='CLASS_TYPE', color='CLASS_TYPE', title='Students by Class',text='STUDENTS')
fig.show()

The above code gives us the following output:

Simple Bar Plot with Plotly Express
Simple Bar Plot with Plotly Express D

You can now see the data labels that tell us the counts of each CLASS_TYPE. Each is placed on the relevant bar ofr ease of reading.✨✨✨

Hopefully you found the above tutorial to be helpful. Find the full source code HERE and check out another great bar plot tutorial HERE. Good luck in creating your Plotly Bar Chart In Python! 👌👌👌

Tags: bar chartbar graphplotly
Previous Post

Stacked Bar Plot with Python

Next Post

Bar Plot with Python and seaborn

Khaleel O.

Khaleel O.

I love to share, educate and help developers. I have 14+ years experience in IT. Currently transitioning from Systems Administration to DevOps. Avid reader, intellectual and dreamer. Enter Freely, Go safely, And leave something of the happiness you bring.

Related Posts

Python

Python Fibonacci Recursive Solution

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Fibonacci Recursive Solution. Let's go! 🔥🔥🔥 The Fibonacci sequence is a series of numbers in which...

Read moreDetails
Python

Python Slice String List Tuple

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Slice string list tuple how-to tutorial. Let's go! 🔥🔥🔥 In Python, a slice is a feature...

Read moreDetails
Python

Python Blowfish Encryption Example

by Khaleel O.
January 14, 2024
0
0

Let's do a Python Blowfish Encryption example. Let's go! 🔥 🔥 Blowfish is a symmetric-key block cipher algorithm designed for...

Read moreDetails
Python

Python Deque Methods

by Khaleel O.
January 14, 2024
0
0

In this post we'll list Python Deque Methods. Ready? Let's go! 🔥🔥🔥 A deque (double-ended queue) in Python is a...

Read moreDetails

DevRescue © 2021 All Rights Reserved. Privacy Policy. Cookie Policy

Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
Manage your privacy
To provide the best experiences, DevRescue.com will use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
No Result
View All Result
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us

DevRescue © 2022 All Rights Reserved Privacy Policy