plotly legend
时间: 2023-09-01 17:10:08 浏览: 80
Plotly is a powerful Python library for creating interactive visualizations. Legends in Plotly are used to provide context and identify the different elements in a plot, such as lines, markers, or colors. To add a legend to a Plotly graph, you can use the `legend` attribute within the layout object.
Here's an example of how you can add a legend to a Plotly graph:
```python
import plotly.graph_objects as go
# Create a scatter plot
fig = go.Figure(data=go.Scatter(x=[1, 2, 3], y=[4, 5, 6], name='Data'))
# Set layout properties
fig.update_layout(
showlegend=True, # Show legend
legend=dict(
x=0, # Legend position (0-1)
y=1.0 # Legend position (0-1)
)
)
# Display the plot
fig.show()
```
In this example, we create a scatter plot with three data points and assign it a name 'Data'. By setting `showlegend` to `True` in the layout, we enable the display of the legend. The `legend` property allows you to customize its position by specifying `x` and `y` values between 0 and 1.
阅读全文
相关推荐
















