plotly

Plotly.express.bar

Ask anyone who has ever done any kind of data visualization and the one plot will tell you that they have used a bar plot.

Bar plots are the most common, apart from line plots, types of plots anyone can create. They show the relationship between a numerical and a categorial value. Each relationship is then shown as a bar with the size height of the bar representing the actual value.

Due to this simplicity, bar plots are fairly easy to create and interpret. This makes them very useful for quick plots.

In this tutorial, we will explore how we can use the Plotly Express module to create bar plots.

Plotly.Expres.bar()

As you have probably guessed, to create bar plot in Plotly, we use the bar() function. The function syntax as shown below:

plotly.express.bar(data_frame=None, x=None, y=None, color=None, pattern_shape=None, facet_row=None, facet_col=None, facet_col_wrap=0, facet_row_spacing=None, facet_col_spacing=None, hover_name=None, hover_data=None, custom_data=None, text=None, base=None, error_x=None, error_x_minus=None, error_y=None, error_y_minus=None, animation_frame=None, animation_group=None, category_orders=None, labels=None, color_discrete_sequence=None, color_discrete_map=None, color_continuous_scale=None, pattern_shape_sequence=None, pattern_shape_map=None, range_color=None, color_continuous_midpoint=None, opacity=None, orientation=None, barmode='relative', log_x=False, log_y=False, range_x=None, range_y=None, text_auto=False, title=None, template=None, width=None, height=None)

Don’t be intimidated by the massive parameter list. The function is very simple to use and you will only need to remember five of the above parameters.

The most useful function parameters that you will need to know include:

  1. Data_frame – specifies the columns used in the plot. You can pass a Pandas DataFrame, a dictionary, or an array_like object.
  2. x – specifies the values used position the marks along the x axis. You can specify this parameter as a column name with a DataFrame, a Pandas Series or a array_like object.
  3. y – similar to x but the values are used to position the marks along the y axis.
  4. color – specifies the values used to assign a unique color the marks.

The function will return the Bar Plot of the specified data as graph_objects.Figure type.

Let us explore how we can use the bar() function to create real bar plots.

Example 1 – Simple Bar Plot

Let us start with a simple bar plot as shown in the code below:

import plotly.express as px

import pandas as pd

df = pd.DataFrame({

"OS": [ 'Android', 'Windows', 'iOS', 'OS X', 'Linux', 'Other'],

"Usage": [44.17,28.96,17.46,5.56,0.92,1.92]

})

fig = px.bar(df, x='OS', y='Usage')

fig.show()

In the example code above, we start by importing the plotly express pandas packages.

We then create a DataFrame and pass the values we wish to plot.

Finally, using the bar() function, we create a bar plot of the OS popularity. The resulting figure is shown:

Notice that each row represents the data of OS. Therefore, each bar represents each browser.

Example 2

For better visibility, we can tell Plotly to assign a unique color for each OS as shown below:

import plotly.express as px

import pandas as pd

df = pd.DataFrame({

"OS": [ 'Android', 'Windows', 'iOS', 'OS X', 'Linux', 'Other'],

"Usage": [44.17,28.96,17.46,5.56,0.92,1.92]

})

fig = px.bar(df, x='OS', y='Usage', color='OS')

fig.show()

The example above sets the color parameter as the OS column in the DataFrame. This should result in a figure as shown:

Example – Horizontal Bar Plot

We can create a horizontal bar plot by switching the columns for x and y axis. An example is as shown:

import plotly.express as px

import pandas as pd

df = pd.DataFrame({

"OS": [ 'Android', 'Windows', 'iOS', 'OS X', 'Linux', 'Other'],

"Usage": [44.17,28.96,17.46,5.56,0.92,1.92]

})

fig = px.bar(df, x='Usage', y='OS', color='OS')

fig.show()

The code above should result in a horizontal bar plot as shown:

Example 4 – Bar Plots with Text

If you wish to set the values inside the bars, you can use the text_auto parameter and set it to True as shown:

import plotly.express as px

import pandas as pd

df = pd.DataFrame({

"OS": [ 'Android', 'Windows', 'iOS', 'OS X', 'Linux', 'Other'],

"Usage": [44.17,28.96,17.46,5.56,0.92,1.92]

})

fig = px.bar(df, x='Usage', y='OS', color='OS', text_auto=True)

fig.show()

The resulting figure is as shown:

Here, we can see the actual values inside the bars.

Closing

In this article, we explored the fundamentals of working with Bar plots in Plotly.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list