Plotly.Express.Area

“An area plot is a specific type of line chart that combines the available data points along the horizontal axis. The area plot then adds color to the area under the connected points. They are very fundamental when you want to illustrate trends without many details in the accuracy.
In this tutorial, we will learn how we can create an area plot using the Plotly Express module.”

Let’s dive in.

Plotly.Express.Area

To create an area plot with Plotly Express, we use the area() function. The function syntax is as shown below:

plotly.express.area(data_frame=None, x=None, y=None, line_group=None, color=None,

pattern_shape=None, symbol=None, hover_name=None, hover_data=None,

custom_data=None, text=None, facet_row=None, facet_col=None, facet_col_wrap=0,

facet_row_spacing=None, facet_col_spacing=None, animation_frame=None,

animation_group=None, category_orders=None, labels=None,

color_discrete_sequence=None, color_discrete_map=None,

pattern_shape_sequence=None, pattern_shape_map=None, symbol_sequence=None,

symbol_map=None, markers=False, orientation=None, groupnorm=None, log_x=False,

log_y=False, range_x=None, range_y=None, line_shape=None, title=None,

template=None, width=None, height=None)

The following are the most important parameters you will need to know:

  1. Data_frame – specifies the column names for the plot. Either as a DataFrame, array_like object, or a dictionary.
  2. X – defines the values used as marks along the x-axis along the cartesian coordinate. This can be a name of a column within a data frame, a Pandas Series, or an array_like object.
  3. Y – similar to x but specifies the marks used along the y axis.
  4. Color – species the values used for color marks. It can be a column name, a pandas series, or an array object.

The function returns an area plot of type graph_objects.Figure.

Example 1

The code below shows how to use the area() function from the Plotly express module to create a simple area graph.

import plotly.express as px
df = px.data.iris()
fig = px.area(df, x='petal_width', y='petal_length')
fig.show()

The code above will fetch the iris data from plotly and create an area t plot with the specified x and y values.

The resulting figure is as shown:

Example 2

We can specify the color parameter as a column within the iris dataframe as shown in the code below:

import plotly.express as px
df = px.data.iris()
fig = px.area(df, x='petal_width', y='petal_length', color='species')
fig.show()

In this case, the function returns an area plot as shown:

Example 3

Take another example shown below:

import plotly.express as px
df = px.data.tips()
fig = px.area(df, x='total_bill', y='day', color='sex')
fig.show()

Here, we are using the tips data from plotly express and setting the color value to the column “sex”.

The code above should return a figure as shown:

And that’s all there is to area plots using plotly express.

Thanks for reading!!



from https://ift.tt/89v3XCN

Post a Comment

0 Comments