How to use Plotly – Chart library

Plotly Visualization Examples

Plotly Visualization Examples

I created some sample codes which can be run in Jupyter Notebook.
These codes will be very useful 🙂

Installation

pip install plotly

1. Line Chart


import plotly.express as px

# Load the Iris dataset
df = px.data.iris()

# Create a line chart with sepal width on the x-axis and sepal length on the y-axis
# Different species are represented by different colors
fig = px.line(df, x="sepal_width", y="sepal_length", color="species", title='Iris Sepal Width vs Length')
fig.show()
    

2. Scatter Plot


import plotly.express as px

# Load the Iris dataset
df = px.data.iris()

# Create a scatter plot with sepal width and sepal length
# The size of points represents petal length, and hover data shows petal width
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", size='petal_length', hover_data=['petal_width'], title='Iris Sepal Width vs Length')
fig.show()
    

3. Bar Chart


import plotly.express as px

# Load the Tips dataset
df = px.data.tips()

# Create a grouped bar chart showing total bill by day and smoker status
fig = px.bar(df, x="day", y="total_bill", color="smoker", barmode="group", title='Total Bill by Day and Smoker Status')
fig.show()
    

4. Histogram


import plotly.express as px

# Load the Tips dataset
df = px.data.tips()

# Create a histogram to show the distribution of total bill amounts
fig = px.histogram(df, x="total_bill", nbins=20, title='Distribution of Total Bill')
fig.show()
    

5. Box Plot


import plotly.express as px

# Load the Tips dataset
df = px.data.tips()

# Create a box plot to compare total bill amounts by day and smoker status
fig = px.box(df, x="day", y="total_bill", color="smoker", title='Box Plot of Total Bill by Day and Smoker Status')
fig.show()
    

6. Heatmap


import plotly.express as px

# Load the Tips dataset
df = px.data.tips()

# Create a heatmap to visualize the relationship between total bill and tip amounts
fig = px.density_heatmap(df, x="total_bill", y="tip", nbinsx=20, nbinsy=20, title='Heatmap of Total Bill vs Tip')
fig.show()
    

7. Pie Chart


import plotly.express as px

# Load the Tips dataset
df = px.data.tips()

# Create a pie chart to show the distribution of tips by day
fig = px.pie(df, values='tip', names='day', title='Distribution of Tips by Day')
fig.show()
    

8. Polar Chart


import plotly.express as px

# Load the Wind dataset
df = px.data.wind()

# Create a polar chart to visualize wind frequency by direction and strength
fig = px.line_polar(df, r="frequency", theta="direction", color="strength", line_close=True, title='Wind Frequency by Direction and Strength')
fig.show()
    

9. 3D Scatter Plot


import plotly.express as px

# Load the Iris dataset
df = px.data.iris()

# Create a 3D scatter plot to visualize sepal width, sepal length, and petal length
fig = px.scatter_3d(df, x='sepal_width', y='sepal_length', z='petal_length', color='species', title='3D Scatter Plot of Iris')
fig.show()
    

10. Map Plot (Choropleth Map)


import plotly.express as px

# Load Gapminder dataset for 2007 data only
df = px.data.gapminder().query("year==2007")

# Create a choropleth map to visualize GDP per capita by country in 2007
fig = px.choropleth(df, locations="iso_alpha", color="gdpPercap", hover_name="country", title='GDP Per Capita by Country in 2007')
fig.show()
    

Leave a Reply

Your email address will not be published. Required fields are marked *