Bar chart using Plotly in Python

Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization library. 

Bar Chart 

In a bar chart the data categories are displayed on the vertical axis and the data values are displayed on the horizontal axis. Labels are easier to display and with a big data set they impel to work better in a narrow layout such as mobile view. 

Syntax: plotly.express.bar(data_frame=None, x=None, y=None, color=None, facet_row=None, facet_col=None, facet_col_wrap=0, hover_name=None, hover_data=None, custom_data=None, text=None, error_x=None, error_x_minus=None, error_y=None, error_y_minus=None, animation_frame=None, animation_group=None, category_orders={}, labels={}, color_discrete_sequence=None, color_discrete_map={}, color_continuous_scale=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, title=None, template=None, width=None, height=None)

Parameters:

Name Description
data_frame  This argument needs to be passed for column names (and not keyword names) to be used. Array-like and dict are tranformed internally to a pandas DataFrame. Optional: if missing, a DataFrame gets constructed under the hood using the other arguments.
x Either a name of a column in data_frame, or a pandas Series or array_like object. Values from this column or array_like are used to position marks along the x axis in cartesian coordinates. Either x or y can optionally be a list of column references or array_likes, in which case the data will be treated as if it were ‘wide’ rather than ‘long’.
y Either a name of a column in data_frame, or a pandas Series or array_like object. Values from this column or array_like are used to position marks along the y axis in cartesian coordinates. Either x or y can optionally be a list of column references or array_likes, in which case the data will be treated as if it were ‘wide’ rather than ‘long’.
color  Either a name of a column in data_frame, or a pandas Series or array_like object. Values from this column or array_like are used to assign color to marks.

Example 1:



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import plotly.express as px
import numpy
  
# creating random data through randomint 
# function of numpy.random 
np.random.seed(42
    
random_x= np.random.randint(1,101,100
random_y= np.random.randint(1,101,100)
  
fig = px.bar(random_x, y = random_y)
fig.show()

chevron_right


Output:

Example 2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import plotly.express as px
  
df = px.data.iris()
  
fig = px.bar(df, x="sepal_width", y="sepal_length")
fig.show()

chevron_right


Output:

Showing Facetted subplots

In plotly, to create the facetted subplots facet_row argument is used, where different values correspond to different rows of the dataframe columns.



Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import plotly.express as px
  
df = px.data.iris()
  
fig = px.bar(df, x="sepal_width", y="sepal_length"
             color="species", barmode="group"
             facet_row="species", facet_col="species_id")
  
fig.show()

chevron_right


Output:

Customizing bar charts

By using keyword arguments the bar mode can be customized. Let’s see the examples given below:

Example 1:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import plotly.express as px
  
df = px.data.iris()
  
fig = px.bar(df, x="sepal_width", y="sepal_length", color="species")
fig.show()

chevron_right


Output:

Example 2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import plotly.express as px
  
df = px.data.iris()
  
fig = px.bar(df, x="sepal_width", y="sepal_length",
             color="species", barmode='group')
fig.show()

chevron_right


Output:




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.