Menu

Creating Interactive Statistical and Geographic Charts

Creating Interactive Statistical and Geographic Charts

Plotly supports charts for comparing distributions, examining relationships, and displaying geographic information.

Creating a Box Plot

A box plot helps compare numerical distributions across groups.

import pandas as pd

import plotly.express as px

df = pd.DataFrame({

"Department": ["IT", "IT", "IT", "Sales", "Sales", "Sales"],

"Salary": [50000, 65000, 70000, 35000, 45000, 55000]

})

fig = px.box(

df,

x="Department",

y="Salary",

color="Department",

title="Salary Distribution by Department"

)

fig.show()

The chart displays the median and quartiles, along with potential outliers. Hovering over observations can provide more information about individual values.

Creating a Heatmap

Heatmaps represent values through color intensity and can be useful for comparing correlations.

import pandas as pd

import plotly.express as px

df = pd.DataFrame({

"Advertising": [1000, 1500, 2000, 2500, 3000],

"Website_Visits": [200, 250, 310, 370, 430],

"Sales": [8000, 10000, 13000, 15000, 18000]

})

correlation = df.corr(numeric_only=True)

fig = px.imshow(

correlation,

text_auto=".2f",

color_continuous_scale="RdBu_r",

zmin=-1,

zmax=1,

title="Correlation Heatmap"

)

fig.show()

The color scale represents correlation values from -1 to +1. The numerical annotations make the relationships easier to compare.

Creating a Geographic Map

Plotly can visualize data associated with geographic locations. For example, a dataset containing country names and sales values can be displayed on a world map.

import pandas as pd

import plotly.express as px

df = pd.DataFrame({

"Country": ["India", "United States", "Germany", "Japan"],

"Sales": [50000, 80000, 45000, 60000]

})

fig = px.choropleth(

df,

locations="Country",

locationmode="country names",

color="Sales",

hover_name="Country",

title="Sales by Country",

color_continuous_scale="Blues"

)

fig.show()

A choropleth map colors geographic regions according to their associated values. Geographic names must match Plotly's location recognition, and some maps may require internet access to retrieve geographic boundaries.


Python for Data Science

VA

Vishalini A



Learn via Video Course

undefined Logo

NaN hrs



Get in Touch For Details