Quick tutorial on creating high-quality bar chart using Python and Plotly
In this simple tutorial, we’ll see how to create a remarkable Bar chart in Python using one of the best APIs for data science and visualization, of course, I’m talking about Plotly a fantastic API for charts.
What is a Bar Chart?
Most people confuse the Bar charts with the Histograms, they are similar from a design point of view but have different meanings and purposes. The histogram has the purpose to show continuous data, data representing measurements on some continuous scale. For example, the weight, height, and age of students in a survey would represent continuous variables. Instead, a Bar Chart has the purpose to visualize categorical data, data representing a category. For example, gender, a car model, nations and so on. Typically the x-axis is the category and the y-axis is the count (or other aggregation measures as average, sum…) in each category. You can configure bar charts in different ways depending on the context, in this brief tutorial we’ll see a simple bar chart, stacked bar chart and nested bar chart.
If you are interested in visualize continuous data
Simple Bar Chart in Plotly
In Plotly, building a bar chart is very easy.
import numpy as np import plotly.graph_objects as go import plotly.offline as py np.random.seed(42) # Create our x and y # X is the labels of the categories labels = ['A', 'B', 'C'] # Y is the count for each category values = np.random.randint(10, 100, size=3) # In plotly charts are named traces trace = go.Bar(x=labels, y=values) layout = go.Layout(title='My Simple Bar Chart', xaxis={'title':'Labels'}, # add x-axis title yaxis={'title':'Random Values'}) # add y-axis title fig = go.Figure(data = [trace], layout=layout) py.plot(fig, filename='simple-bar-chart')
The previous code is the simplest bar chart that you can make with Plotly. In particular, the class Bar of the package graph_objects takes:
- X: the category labels
- Y: aggregation measures for each category, like the average, count, sum and so on.
You can refer to the documentation to discover other parameters and customize your bar chart.
Maybe you are interested in build a line chart
Plotly Nested Bar Chart
Nested bar charts are useful when you have to visualize a cross table. For example, suppose to have the following data.
Our goal is to compare the three series along the categories A, B and C.
import numpy as np import plotly.graph_objects as go import plotly.offline as py np.random.seed(42) # Categories labels = ['A', 'B', 'C'] # Generate the first series values values = np.random.randint(10, 100, size=3) np.random.seed(40) # Generate the second series values values2 = np.random.randint(10, 100, size=3) np.random.seed(41) # Generate the third series values values3 = np.random.randint(10, 100, size=3) # The first bar chart representing the first series trace1 = go.Bar(x=labels, y=values, marker={'color': 'orchid'}, selectedpoints=[np.argmax(values)], selected={'marker':{'color': 'orange'}}, texttemplate='%{y}', name='Series A',showlegend=True) # The second bar chart representing the second series trace2 = go.Bar(x=labels, y=values2, marker={'color': 'mediumslateblue'}, selectedpoints=[np.argmax(values2)], selected={'marker':{'color': 'orange'}}, texttemplate='%{y}', name='Series B',showlegend=True) # The third bar chart representing the third series trace3 = go.Bar(x=labels, y=values3, marker={'color': 'rebeccapurple'}, selectedpoints=[np.argmax(values3)], selected={'marker':{'color': 'orange'}}, texttemplate='%{y}', name='Series C', showlegend=True) # Combining the three bar charts traces = [trace1, trace2, trace3] layout = go.Layout(title='My Nested Bar Chart', xaxis={'title':'Labels'}, yaxis={'title':'Random Values'}) fig = go.Figure(data=traces, layout=layout) py.plot(fig, filename='nested-bar-chart')
As a result, the bars are near each other. The violet bars come from trace1, the purples from trace3 and so on. Furthermore, I decided to use the orange colour to highlight the maximum value for each series. Then we will see how to reach this effect.
Plotly Stacked Bar Chart
The code for a stacked bar chart is similar to the nested bar chart, what is the difference? You should change the parameter bar mode of the Layout class to “stack“. Let’s see an example.
import numpy as np import plotly.graph_objects as go import plotly.offline as py np.random.seed(42) labels = ['A', 'B', 'C'] values = np.random.randint(10, 100, size=3) np.random.seed(40) values2 = np.random.randint(10, 100, size=3) np.random.seed(41) values3 = np.random.randint(10, 100, size=3) trace1 = go.Bar(x=labels, y=values, marker={'color': 'orchid'}, texttemplate='%{y}', name='Series A',showlegend=True) trace2 = go.Bar(x=labels, y=values2, marker={'color': 'mediumslateblue'}, texttemplate='%{y}', name='Series B',showlegend=True) trace3 = go.Bar(x=labels, y=values3, marker={'color': 'rebeccapurple'}, texttemplate='%{y}', name='Series C', showlegend=True) traces = [trace1, trace2, trace3] layout = go.Layout(title='My Stacked Bar Chart', xaxis={'title':'Labels'}, yaxis={'title':'Random Values'}, barmode='stack') # stack mode fig = go.Figure(data = traces, layout=layout)
Sophisticated Bar Chart in Potly
Now, let’s play with parameters customizing better our bar chart.
import numpy as np import plotly.graph_objects as go import plotly.offline as py np.random.seed(42) labels = ['A long label', 'Another Long Label', 'Finally the last long label'] values = np.random.randint(10, 100, size=3) trace = go.Bar(x=values, y=labels, orientation='h', marker={'color': 'lightslategrey'}, selectedpoints=[np.argmax(values)], selected={'marker':{'color': 'orange'}}) style = '<b style="color:orange;">Showing max value between categories</b>' layout = go.Layout(title='Sophisticated Bar Chart: ' + style, xaxis={'title':'Labels'}, yaxis={'title':'Random Values'}) fig = go.Figure(data=[trace], layout=layout) i = 0 for value in values: fig.add_annotation(x=value, y=i, text="<b>" + str(value) + "</b>", showarrow=False, align='left', width=50, font={'size': 20}) i = i + 1 py.plot(fig, filename='custom-bar-chart')
As a result, you can see that this chart is a little bit more sophisticated than the previous ones. Indeed, the chart is enriched with more information useful for our audience. Let’s analyze step-by-step the code.
Bar Chart: marker and orientation
Since I wanted to show also the horizontal bar chart I’ve set the orientation to horizontal. Then, I’ve coloured the bars (light slate grey) with the marker parameter. In particular, this parameter takes as an argument a dictionary-like structure where to set different attributes of the bar (refer to documentation). Note that when you chose horizontal orientation X and Y are inverted.
labels = ['A long label', 'Another Long Label', 'Finally the last long label'] values = np.random.randint(10, 100, size=3) trace = go.Bar(x=values, y=labels, orientation='h', marker={'color': 'lightslategrey'}, selectedpoints=[np.argmax(values)], selected={'marker':{'color': 'orange'}})
Select the max values from categories
One of the interesting parameters in Plotly classes is the selected points used to select the points that we want to work on. This parameter takes an array-like structure containing the indexes of the selected points. In this case, I have highlighted the bar showing the maximum value.
selectedpoints=[np.argmax(values)]
Furthermore, we define the style of the selected bar with the parameter selected that takes a dictionary structure as an argument (refer to documentation).
The Layout
To explicitly link the chart title with the chart and its purpose, I’ve played with layout defining a style for the title using the HTML notation. To clarify, this is just to show you that you can use HTML tags and attributes to customize more your labels and titles.
style = '<b style="color:orange;">Showing max value between categories</b>' layout = go.Layout(title='Sophisticated Bar Chart: ' + style, xaxis={'title':'Labels'}, yaxis={'title':'Random Values'})
Adding Text
To add the text you can simply use the parameter text template as you can see in the following code.
# The complete bar chart with text trace = go.Bar(x=values, y=labels, orientation='h', marker={'color': 'lightslategrey'}, selectedpoints=[np.argmax(values)], selected={'marker':{'color': 'orange'}}, texttemplate='%{x}')
Annotations Text
Due to I had trouble with chart studio (I use chart studio to share the charts here) when it comes to adding text to bars, I had to use annotations, it was great because it allowed me to discover and explore more Plotly learning a new little thing.
i = 0 for value in values: fig.add_annotation(x=value, y=i, text="<b>" + str(value) + "</b>", showarrow=False, align='left', width=50, font={'size': 20}) i = i + 1
The class Figure has the method add_annotation to annotate your chart as you prefer. The parameters are a lot, the following are those used:
- X: sets the annotation’s x position. In our case, due to we have categories X assumes numbers as value
- Y: sets the annotation’s y position. Again, due to we have categories Y assumes numbers as value, in this case from 0 to 2 (we have three categories)
- Showarrow: Determines whether or not the annotation is drawn with an arrow. Default is True.
- Align: Sets the horizontal alignment of the text. Has an effect if an explicit width is set to override the text width
- Width: set the imaginary width box in which the text is positioned
- Font: define the font of the text. It takes a dict-like structure.
Refer to the documentation for more details.
Maybe you are interested also in building scatter plots
Conclusion
To conclude, Plotly is an easy-to-use library to create wonderful and interactive visualization and consequently beautiful dashboards.
See you in the next article!
You might also like
More from Data Visualisation
Plotly – Wonderful Line chart in Python
Brief Python tutorial on line chart in Plotly
Plotly – Make your scatterplot wonderful in Python
Easy way to create a wonderful scatter plot in Python with Plotly.