How to Create an Area Chart in Plotly Python?

Spread the love

An area chart or area graph displays graphically quantitative data. It is based on the line chart. The area between axis and line are commonly emphasized with colors, textures and hatching. Commonly one compares two or more quantities with an area chart.

Reading Data –

Let’s read a dataset to work with.

import pandas as pd
url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/stocks.csv'
df = pd.read_csv(url, parse_dates=['Date'])
df.drop('Unnamed: 0', axis=1, inplace=True)
df.head()

Create an Area Chart with Plotly Express –

To create an Area Chart in Plotly Express we use the px.area function.

import plotly.express as px
fig = px.area(df, x='Date', y='Open', color='Symbol')
fig.show()

Create an Area Chart with Plotly Graph Objects –

We can also create an Area chart with plotly graph objects using the go.Scatter function.

# stock data for different companies
apple_df = df[df['Symbol']=='AAPL']
google_df = df[df['Symbol']=='GOOG']
tesla_df = df[df['Symbol']=='TSLA']

# create area chart
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(x = apple_df['Date'], y =apple_df['Open'], stackgroup='one', name='AAPL'))
fig.add_trace(go.Scatter(x = google_df['Date'], y =google_df['Open'], stackgroup='one', name='GOOG'))
fig.add_trace(go.Scatter(x = tesla_df['Date'], y =tesla_df['Open'], stackgroup='one', name='TSLA'))
fig.show()

Related Posts –

  1. How to install plotly python with pip?
  2. How to create a Line Chart with Plotly Python?
  3. How to create Scatter plot in Plotly Python?
  4. How to create a Bar Chart in Plotly Python?
  5. How to create Horizontal Bar Chart in Plotly Python?
  6. How to create a Histogram in plotly python?
  7. How to Create a Box Plot in Plotly Python?
  8. How to create a Pie Chart in Plotly Python?
  9. How to create a Dot Plot in Plotly Python?
  10. How to Create Heatmap with Plotly Python?
  11. How to Create a Violin Plot in Plotly Python?
  12. How to Create Subplots in Plotly Python?
  13. How to Create a Bubble Chart in Plotly Python?
  14. How to Create a Gantt Chart in Plotly Python?

Rating: 1 out of 5.

Leave a Reply