Skip to content

Latest commit

 

History

History
119 lines (88 loc) · 4.17 KB

log-plot.md

File metadata and controls

119 lines (88 loc) · 4.17 KB
jupyter
jupytext kernelspec language_info plotly
notebook_metadata_filter text_representation
all
extension format_name format_version jupytext_version
.md
markdown
1.3
1.13.7
display_name language name
Python 3 (ipykernel)
python
python3
codemirror_mode file_extension mimetype name nbconvert_exporter pygments_lexer version
name version
ipython
3
.py
text/x-python
python
python
ipython3
3.9.7
description display_as language layout name order permalink redirect_from thumbnail
How to make Log plots in Python with Plotly.
scientific
python
base
Log Plots
5
python/log-plot/
python/log-plots/
thumbnail/log.jpg

This page shows examples of how to configure 2-dimensional Cartesian axes to follow a logarithmic rather than linear progression. Configuring gridlines, ticks, tick labels and axis titles on logarithmic axes is done the same was as with linear axes.

Logarithmic Axes with Plotly Express

Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures.

All of Plotly Express' 2-D Cartesian functions include the log_x and log_y keyword arguments, which can be set to True to set the corresponding axis to a logarithmic scale:

import plotly.express as px
df = px.data.gapminder().query("year == 2007")

fig = px.scatter(df, x="gdpPercap", y="lifeExp", hover_name="country", log_x=True)
fig.show()

Setting the range of a logarithmic axis with Plotly Express works the same was as with linear axes: using the range_x and range_y keywords. Note that you cannot set the range to include 0 or less.

import plotly.express as px
df = px.data.gapminder().query("year == 2007")

fig = px.scatter(df, x="gdpPercap", y="lifeExp", hover_name="country",
                 log_x=True, range_x=[1,100000], range_y=[0,100])
fig.show()

Adding minor ticks

new in 5.8

You can position and style minor ticks using minor. This takes a dict of properties to apply to minor ticks. See the figure reference for full details on the accepted keys in this dict.

In this example we set the tick length with ticklen, add the ticks on the inside with ticks="inside", and turn grid lines on with howgrid=True.

import plotly.express as px
df = px.data.gapminder().query("year == 2007")

fig = px.scatter(df, x="gdpPercap", y="lifeExp", hover_name="country",
                 log_x=True, range_x=[1,100000], range_y=[0,100])

fig.update_xaxes(minor=dict(ticks="inside", ticklen=6, showgrid=True))

fig.show()

Logarithmic Axes with Graph Objects

If Plotly Express does not provide a good starting point, it is also possible to use the more generic go.Figure class from plotly.graph_objects.

import plotly.graph_objects as go
import plotly.express as px
df = px.data.gapminder().query("year == 2007")

fig = go.Figure()

fig.add_trace(go.Scatter(mode="markers", x=df["gdpPercap"], y=df["lifeExp"] ))

fig.update_xaxes(type="log")
fig.show()

Setting the range of a logarithmic axis with plotly.graph_objects is very different than setting the range of linear axes: the range is set using the exponent rather than the actual value:

import plotly.graph_objects as go
import plotly.express as px
df = px.data.gapminder().query("year == 2007")

fig = go.Figure()

fig.add_trace(go.Scatter(mode="markers", x=df["gdpPercap"], y=df["lifeExp"] ))

fig.update_xaxes(type="log", range=[0,5]) # log range: 10^0=1, 10^5=100000
fig.update_yaxes(range=[0,100]) # linear range
fig.show()

Reference

See function reference for px.(scatter) or https://plotly.com/python/reference/layout/xaxis/#layout-xaxis-type for more information and chart attribute options!