How to mention custom daterange using polars Dataframe in python plotly plots?
Question:
I am new to python polars
and trying to create a line plot
using plotly express
with custom start date
& max date
as the x-axis date range of the plot.
Doubt: Do I need to pass the whole polars dataframe
again in fig_pop.update_layout(xaxis_range = )
to set the date range and do the entire df
processing again or is there an alternative way to this as I already passed the dataframe
to the figure object
?
My code attempt:
Data processing
import polars as pl
import pandas as pd
import plotly.express as px
import datetime as dt
# creating pandas df
df_gapminder = px.data.gapminder()
df_gapminder['date'] = pd.to_datetime(df_gapminder.year, format="%Y")
# polars dataframe
pl_gapminder = pl.DataFrame(df_gapminder.query('country == "India"'))
pl_gapminder = pl_gapminder.with_columns(pl.col('date').cast(pl.Date))
Doubt in fig_pop.update_layout(xaxis_range = )
below section of code
################# Plotting Below #################
fig_pop = px.line(pl_gapminder.select(pl.col(['country','date','gdpPercap'])
).to_pandas(),
x='date',y='gdpPercap',title="India's GDPPerCap Growth")
fig_pop.update_xaxes(rangeslider_visible = True)
# DOUBT in below line: do we need to pass whole dataset again or there is a better way?
fig_pop.update_layout(xaxis_range = ['1977-01-01',pl_gapminder.select(pl.col('date')).max().item()],
showlegend = False,
title = {'x':0.5},
plot_bgcolor = "rgba(0,0,0,0)",
xaxis = (dict(showgrid = False)),
yaxis = (dict(showgrid = False)),)
fig_pop.show()
Answers:
No you don’t need to pass the whole dataframe again.
Actually xaxis_range
is a shortcut that uses the magic underscore notation for referencing the range
(nested) property of xaxis
. So it is set correctly, the problem you have is that setting xaxis=(...)
in the same call overrides it.
You can do either :
fig_pop.update_layout(
showlegend=False,
title={'x':0.5},
plot_bgcolor="rgba(0,0,0,0)",
xaxis=dict(
showgrid=False,
range=['1977-01-01', pl_gapminder['date'].dt.max()]
),
yaxis=dict(showgrid=False)
)
Or keep using underscore notation :
fig_pop.update_layout(
xaxis_range=['1977-01-01', pl_gapminder['date'].dt.max()],
showlegend=False,
title={'x':0.5},
plot_bgcolor="rgba(0,0,0,0)",
xaxis_showgrid=False,
yaxis_showgrid=False
)
I am new to python polars
and trying to create a line plot
using plotly express
with custom start date
& max date
as the x-axis date range of the plot.
Doubt: Do I need to pass the whole polars dataframe
again in fig_pop.update_layout(xaxis_range = )
to set the date range and do the entire df
processing again or is there an alternative way to this as I already passed the dataframe
to the figure object
?
My code attempt:
Data processing
import polars as pl
import pandas as pd
import plotly.express as px
import datetime as dt
# creating pandas df
df_gapminder = px.data.gapminder()
df_gapminder['date'] = pd.to_datetime(df_gapminder.year, format="%Y")
# polars dataframe
pl_gapminder = pl.DataFrame(df_gapminder.query('country == "India"'))
pl_gapminder = pl_gapminder.with_columns(pl.col('date').cast(pl.Date))
Doubt in fig_pop.update_layout(xaxis_range = )
below section of code
################# Plotting Below #################
fig_pop = px.line(pl_gapminder.select(pl.col(['country','date','gdpPercap'])
).to_pandas(),
x='date',y='gdpPercap',title="India's GDPPerCap Growth")
fig_pop.update_xaxes(rangeslider_visible = True)
# DOUBT in below line: do we need to pass whole dataset again or there is a better way?
fig_pop.update_layout(xaxis_range = ['1977-01-01',pl_gapminder.select(pl.col('date')).max().item()],
showlegend = False,
title = {'x':0.5},
plot_bgcolor = "rgba(0,0,0,0)",
xaxis = (dict(showgrid = False)),
yaxis = (dict(showgrid = False)),)
fig_pop.show()
No you don’t need to pass the whole dataframe again.
Actually xaxis_range
is a shortcut that uses the magic underscore notation for referencing the range
(nested) property of xaxis
. So it is set correctly, the problem you have is that setting xaxis=(...)
in the same call overrides it.
You can do either :
fig_pop.update_layout(
showlegend=False,
title={'x':0.5},
plot_bgcolor="rgba(0,0,0,0)",
xaxis=dict(
showgrid=False,
range=['1977-01-01', pl_gapminder['date'].dt.max()]
),
yaxis=dict(showgrid=False)
)
Or keep using underscore notation :
fig_pop.update_layout(
xaxis_range=['1977-01-01', pl_gapminder['date'].dt.max()],
showlegend=False,
title={'x':0.5},
plot_bgcolor="rgba(0,0,0,0)",
xaxis_showgrid=False,
yaxis_showgrid=False
)