How to make Log plots in Python with Plotly.
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.
In [1]:
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.
In [2]:
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
.
In [3]:
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()Controlling Minor Log Labels¶
New in 6.3
You can control how minor log labels are displayed using the minorloglabels
attribute. Set to "complete"
to show complete digits, or None
for no labels. By default, minor log labels use "small digits"
, as shown in the previous example.
In [4]:
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 ), minorloglabels="complete" ) fig.show()
In [5]:
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:
In [6]:
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()What About Dash?¶
Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash at https://dash.plot.ly/installation.
Everywhere in this page that you see fig.show()
, you can display the same figure in a Dash application by passing it to the figure
argument of the Graph
component from the built-in dash_core_components
package like this:
import plotly.graph_objects as go # or plotly.express as px fig = go.Figure() # or any Plotly Express function e.g. px.bar(...) # fig.add_trace( ... ) # fig.update_layout( ... ) from dash import Dash, dcc, html app = Dash() app.layout = html.Div([ dcc.Graph(figure=fig) ]) app.run(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4