Plotly Express
Visual expression of your data
A more visually appealing and interactive output. Where pandas and Matplotlib give out a basic chart for looking at.
Plotly can have you zoom in, zoom out, pan, select and download the plot. Plotly can even make 3d plots that you can move around and see your plot rotate in 3d.
To import plotly express, it is usually called in with an alias “px”.
import plotly.express as pxScatter plot
A scatter plot is the same everywhere. If requested, I will write specifically on the scatter plot.
To create a scatter plot.
fig = px.scatter(df, x = 'price', y = 'area')
fig.show()The x and y axes will give the two axes of the 2D scatter plot. To create a 3D scatter plot, you will assign the x, y, and z arguments.
fig = px.scatter(df, x='price',y='height',z='property_type')
fig.show()Note that with a scatter plot, you can add extra dimensionality with shape and color. That’s to say, if you are making a plot, you can have different colors, which will help you make better comparisons. Like houses or apartments in different shapes, or countries in different colors.
fig = px.scatter_3d(
df,
x='price',
y='height',
z='area',
color='country',
shape='property_type'
labels = {'weight' = 'Weight [kg]',
'height' = 'Height [cm]',
'area' = 'Surface area[m2]'}
)
fig.show()Your output will show a 3-dimensional plot with price, height, and area on each axis. The shape of each point will be determined by the type of property, and the color will be determined by the country.
Be careful when using a 3D scatter plot. If there is too much information in the plot, it may become hard to read.
Scatter map box
This is similar to a 2d scatter plot, but rather than have points distributed based on numerical values, the points are distributed based on geographical location.
If you are working with housing data, latitude and longitude values can be passed, and you will get a plot of the houses on a map.
fig = px.scatter_mapbox(
df,
lat = 'latitude',
lon = 'longitude',
)The style of the map can be set by assigning the style to mapbox_style
fig = px.scatter_mapbox(
df,
lat = 'latitude',
lon = 'longitude',
mapbox_style = "carto-positron"
)The various styles of Mapbox are listed below. Feel free to try them all out.
Choropleth map
This shows a distribution across locations, with the saturation of the color proportional to the variable of interest.
Think of showing election voting results across states in a country where the color depth will be determined by the number of votes, or if you are measuring temperature or rainfall levels across countries in the world.
It can be used in cases of a pandemic or disease outbreak. The more saturated the location, the more cases present in that region.
Get I. Jesse’s stories in your inbox
Join Medium for free to get updates from this writer.
The choropleth can take its location in different formats.
df = pd.DataFrame.from_dict(
{
"State": ["CA", "TX", "NY", "HI", "DE"],
"Temparature": [100, 120, 110, 90, 105]}
)
fig = px.choropleth(
df, locations="State", locationmode="USA-states", color="Temparature", scope="usa"
)
fig.show()- Location mode: This determines the location used to match the locations assigned to the choropleth function.
- lat / lon: You can assign locations based on latitudinal and longitudinal values.
- Locations: You can also just assign the locations directly. The country names have to be the location ID.
Follow me if you want to get more explanation on the choropleth map function.
Histogram
The classic histogram, but with the interactive interface of Plotly Express.
fig = px.histogram(df, x="price")
fig.show()Box plot
fig = px.box(df, y='price')
fig.show()Plotly box plot can have you plot multiple box plots across different categories in one graph output.
fig = px.box(df, x='country', y='price')This gives a different box plot for prices in each country in your data, all in the same chart.
Bar chart
fig = px.bar(df['country'].value_counts())
fig.show()For a more expressive bar chart, more arguments to change the way the output displays.
fig = px.bar(size_df, x="country", y="size", color="property_type", barmode="relative")
fig.show()The bar mode determines how the bars of the bar chart will be grouped. Various modes to group include (’group’, ’overlay’, or ’relative’).
In ’relative’ mode, bars are stacked above zero for positive values and below zero for negative values. In ’overlay’ mode, bars are drawn on top of one another. In ’group’ mode, bars are placed beside each other.
There are various Mapbox styles, some of which require an API token. Allowed values that require a Mapbox API token are
`'open-street-map'`,
`'white-bg'`,
`'carto-positron'`,
`'carto-darkmatter'`,
`'stamen-terrain'`,
`'stamen-toner'`,
`'stamen-watercolor'`.
`'basic'`,
`'streets'`,
`'outdoors'`,
`'light'`,
`'dark'`,
`'satellite'`,
`'satellite-streets'`.Hover Data
Plotly has the option to input hover data. Hover data is simply the data that will show when you hover over the points and can be inserted in all types of plots.. This can help make your visualization easy to understand.
For example. If you are searching for a house and have created a plot for house price and land area on a map. Your hover data could include the year it was built and the contact of the agent to call.
If you know more about the topic, contribute in the comments.
If you want to know more, ask any questions in the comments.
Show appreciation by clapping for the story.
Help others find the story by sharing it.
Click the follow button. If you would like to see more, and encourage me to keep posting more.






