plotly / plotly_express

Plotly Express - Simple syntax for complex charts. Now integrated into plotly.py!
https://plot.ly/python/plotly-express/
MIT License
4 stars 0 forks source link

Passing dictionary options during chart invocation #150

Closed s2t2 closed 4 years ago

s2t2 commented 4 years ago

Hi, I'm making charts like a violin plot, and I have need to conditionally assemble its options. I was hoping to be able to pass a dictionary to represent the optional parameters during chart construction.

Example invocation that works:

import plotly.express as px

 # df is my data frame

fig = px.violin(df, x="mycol", y="othercol", title="My title", orientation="v", box=True, points=None)

Example of desired invocation (currently throws error "Value of 'x' is not the name of a column in 'data_frame'..."):

import plotly.express as px

 # df is my data frame

plot_opts = {"x": "my_col", "y": "other_col", "title": "My Title", "orientation": "v", "box": True, "points": None}

fig = px.violin(df, plot_opts)

Is there a way to pass the options as a dictionary? Thanks!

DrGFreeman commented 4 years ago

Hi @s2t2,

You can simply "unpack" your keyword arguments using **kwargs.

plot_opts = {"x": "my_col", "y": "other_col", "title": "My Title", "orientation": "v", "box": True, "points": None}

fig = px.violin(df, **plot_opts)
s2t2 commented 4 years ago

@DrGFreeman it works! Thanks for teaching me something new.