Here in this post we will plot stacked bar chart using plotly in Python. As everyone say that data is the new oil, but on top of that i can say that data visualization is the tool which adds importance to that oil and without that oil aka data is of no use.
Lets plot the tacked bar chart now.
Code to plot the chart :
Lets first import the libraries which we need to plot the charts, which basically are:
- Plotly for data visualization
- Pandas for data read and data manipulation
import plotly.express as px import pandas as pd import plotly.graph_objs as go
Now lets read the data and print the data
data= pd.read_csv(r"C:\Users\WeirdGeek\Desktop\Students.csv") data.head(20)
Here’s how our data looks like:
Now let’s see the Plotly code to plot stacked bar chart:
fig = px.bar(data,x='Name ', y='Marks', color='Subject',height=400, width=700) fig.show()
The above code will take the name column on the x axis and marks column on the y axis and to add the color which finally prepare the stacked bar chart we add the Subject to the color attribute. Also, we provided the height and width of the chart.
Here’s how the charts looks like:
To get more detailed knowledge about charts in Plotly you can check the official website Plotly by Dash. Also if you want to explore more codes/posts related to Data visualization on our website, then you can find it here.
If you have any questions or if you need any help please get in touch with me using the comment section below.
Patryk says
Why did you import plotly.graph_objs as go as you did not use that in the code?