My question might seem very easy, but I am having a difficult time understanding how to create a scatter plot or line plot for only one group of values. For example, my data frame, has 3 columns. My table looks like the following:
fruit | lb | price |
---|---|---|
orange | 1 | 1.4 |
orange | 2 | 1.7 |
apple | 3 | 2.1 |
apple | 1 | 1.4 |
kiwi | 2 | 1.1 |
I want to create a scatter plot that has the lb as the x axis and price as the y axis. However, I only want to make the plot only for the orange category. What parameter should I use to specify the orange category?
What I have now is this:
px.scatter(df, x=df.lb, y=df.price)
Adding a user selection dropdown will accomplish your goal. Use a graph object to draw a graph for each type of fruit and show the Show/Hide setting. All and only each type will be available as a type of dropdown. Give the list of Show/Hide as input for the button. Now, the drop-down selection will toggle between show and hide. Please refer to the examples in the reference.
import plotly.graph_objects as go
fig = go.Figure()
for f in df['fruit'].unique():
dff = df.query('fruit == @f')
fig.add_trace(go.Scatter(mode='markers', x=dff.lb, y=dff.price, name=f, visible=True))
fig.update_layout(
updatemenus=[
dict(
active=0,
buttons=list([
dict(label="ALL",
method="update",
args=[{"visible": [True, True, True]},
{"title": "All fruit"}]),
dict(label="Orange",
method="update",
args=[{"visible": [True, False, False]},
{"title": "Orange"}]),
dict(label="Apple",
method="update",
args=[{"visible": [False, True, False]},
{"title": "Apple"}]),
dict(label="Kiwi",
method="update",
args=[{"visible": [False, False, True]},
{"title": "Kiwi"}]),
]),
)
])
fig.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.