We have seen in our previous posts how we can create 100 % stacked column chart in Tableau. Now in this post we will see how we can create 100 percent stacked column chart using plotly in Python.
Plotly is one of the best library for creating the interactive visualization charts. Its pretty easy to host it to any cloud platform to share it with other people. Now lets easy it is to create the chart using plotly in Python.
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 import the data using the pandas libraries and check the column names as we need them while plotting the chart.
data= pd.read_csv(r"C:\Users\WeirdGeek\Desktop\Students.csv") data.columns
So, now lets try to print the top five columns to get an idea about the above data set:
data.head()
To bring the data in the above format we use Pandas library and its data manipulation power. Let’s see each steps one by one.
- First we bring the rows into columns by using pandas pivot_table function.
- Then, we summed up the total marks in one column
- Then finally we created new column for each subject for calculating the percentage of each subject. i.e. marks in one subject divided by total marks obtained.
Now let’s use the above formatted data into plotly library to plot the chart.
fig = go.Figure() fig.add_trace(go.Bar( y=data["Maths %"], x=data.Name, name="Maths %", marker=dict( color='rgba(0,128,0, 0.6)', line=dict(color='rgba(0,128,0, 0.5)', width=0.05) ) )) fig.add_trace(go.Bar( y=data["Social Science%"], x=data.Name, name="Social Science %", marker=dict( color='rgba(0,0,255, 0.6)', line=dict(color='rgba(0,0,255, 0.5)', width=0.05) ) )) fig.add_trace(go.Bar( y=data["Economics %"], x=data.Name, name="Economics %", marker=dict( color='rgba(128,0,0, 0.5)', line=dict(color='rgba(128,0,0, 0.5)', width=0.05) ) )) fig.update_layout( yaxis=dict( title_text="Marks %", ticktext=["0%", "20%", "40%", "60%","80%","100%"], tickvals=[0, 20, 40, 60, 80, 100], tickmode="array", titlefont=dict(size=15), ), autosize=False, width=1000, height=400, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', title={ 'text': "Students Marks %", 'y':0.96, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'}, barmode='stack') fig.show()
This is how the final chart looks like:
In the above code we have used the generic function go.Bar from plotly.graph_objects. Then added the x and y data to the respective place and choose the color (RGB code) along with the width. then in update_layout() function, we add few parameters like, chart size, Title and its x and y coordinates, and finally the barmode which is the “stack” as we are here plotting the stacked bar chart. Then, finally final line of code to show the figure.
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.
Leave a Reply