110 lines
3.5 KiB
Markdown
110 lines
3.5 KiB
Markdown
For real-time data analysis and interactive visualization in Jupyter notebooks, `Plotly` is an indispensable Python library. Plotly's strength lies in its ability to produce publication-quality graphs with just a few lines of code, supporting over 40 unique chart types covering a wide range of statistical, financial, geographic, scientific, and 3-dimensional use-cases.
|
|
|
|
Here's a concise reference guide for common use cases with `Plotly`, particularly aimed at those new to the library or seeking a quick overview:
|
|
|
|
# `Plotly` Reference Guide
|
|
|
|
## Installation
|
|
```
|
|
pip install plotly
|
|
```
|
|
Note: For Jupyter notebook support, installing the `notebook` and `ipywidgets` packages might also be necessary.
|
|
|
|
## Basic Plotting
|
|
|
|
### Importing Plotly
|
|
```python
|
|
import plotly.graph_objects as go
|
|
```
|
|
|
|
### Creating a Simple Line Chart
|
|
```python
|
|
fig = go.Figure()
|
|
|
|
# Add a trace
|
|
fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[10, 11, 12, 13], mode='lines+markers', name='Line Plot'))
|
|
|
|
# Set title
|
|
fig.update_layout(title='Simple Line Chart')
|
|
|
|
# Show figure
|
|
fig.show()
|
|
```
|
|
|
|
### Creating a Bar Chart
|
|
```python
|
|
data = {
|
|
'x': ['A', 'B', 'C'],
|
|
'y': [120, 150, 100]
|
|
}
|
|
|
|
fig = go.Figure(data=go.Bar(x=data['x'], y=data['y']))
|
|
fig.update_layout(title_text='Basic Bar Chart')
|
|
fig.show()
|
|
```
|
|
|
|
## Interactive Visualizations
|
|
|
|
### Adding Interactivity
|
|
Plotly's figures are interactive by default, with zooming, panning, and hover details. Further customization can enhance user interaction.
|
|
|
|
### Dropdown Menus
|
|
```python
|
|
fig.update_layout(
|
|
updatemenus=[
|
|
dict(
|
|
buttons=list([
|
|
dict(args=["type", "scatter"], label="Scatter", method="restyle"),
|
|
dict(args=["type", "bar"], label="Bar", method="restyle")
|
|
]),
|
|
direction="down",
|
|
pad={"r": 10, "t": 10},
|
|
),
|
|
]
|
|
)
|
|
```
|
|
|
|
## Advanced Plot Types
|
|
|
|
### 3D Plots
|
|
```python
|
|
fig = go.Figure(data=[go.Mesh3d(x=(70, 71, 72), y=(70, 71, 72), z=(1, 2, 3), color='blue', opacity=0.50)])
|
|
fig.update_layout(title_text="3D Mesh example")
|
|
fig.show()
|
|
```
|
|
|
|
### Heatmaps
|
|
```python
|
|
fig = go.Figure(data=go.Heatmap(z=[[1, 20, 30], [20, 1, 60], [30, 60, 1]]))
|
|
fig.update_layout(title='Basic Heatmap')
|
|
fig.show()
|
|
```
|
|
|
|
## Styling and Layout
|
|
|
|
### Customizing Layouts
|
|
```python
|
|
fig.update_layout(
|
|
title='Plot Title',
|
|
xaxis_title='X Axis Title',
|
|
yaxis_title='Y Axis Title',
|
|
legend_title='Legend Title',
|
|
font=dict(
|
|
family="Courier New, monospace",
|
|
size=18,
|
|
color="RebeccaPurple"
|
|
)
|
|
)
|
|
```
|
|
|
|
## Exporting and Saving Plots
|
|
```python
|
|
fig.write_image("fig1.jpeg") # Requires the `kaleido` package
|
|
```
|
|
|
|
`Plotly` excels in making sophisticated and interactive visualizations accessible, with extensive documentation and community examples to help users get started and to solve complex visualization problems. Its seamless integration with Jupyter notebooks makes it a favorite for data analysis and presentation in scientific computing and business intelligence.
|
|
|
|
Plotly's ability to generate web-ready interactive visualizations also makes it ideal for sharing insights with a non-technical audience, ensuring that data-driven stories are engaging and understandable. This guide introduces the core functionality, but Plotly's comprehensive API supports much more, including detailed configuration options for fine-tuning the appearance and behavior of plots.
|
|
|
|
|
|
Plotly's blend of power and simplicity in creating interactive charts makes it a valuable tool for data scientists and analysts looking to explore and present their data dynamically. |