> ## Documentation Index
> Fetch the complete documentation index at: https://ai.tharung.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Plotly

## 1. Introduction to Plotly

**Plotly** is a powerful Python library used for creating **interactive**, **publication-quality**, and **web-based visualizations**.

### Features

* Interactive graphs (zoom, pan, hover)
* 2D and 3D plotting
* Animation support
* Statistical charts
* Geographic maps
* Dashboard integration with Dash
* Export graphs as HTML, PNG, PDF, SVG

***

# 2. Installation

```bash theme={null}
pip install plotly
```

For Jupyter Notebook:

```bash theme={null}
pip install notebook
```

Import Plotly:

```python theme={null}
import plotly.express as px
import plotly.graph_objects as go
```

***

# 3. Plotly Modules

| Module                        | Purpose                 |
| ----------------------------- | ----------------------- |
| plotly.express (px)           | Quick plotting          |
| plotly.graph\_objects (go)    | Advanced customization  |
| plotly.subplots               | Multiple plots          |
| plotly.figure\_factory        | Special charts          |
| [plotly.io](http://plotly.io) | Save and export figures |

***

# 4. Plotly Express

Plotly Express is the easiest interface.

Example:

```python theme={null}
import plotly.express as px

df = px.data.iris()

fig = px.scatter(df,
                 x="sepal_width",
                 y="sepal_length",
                 color="species")

fig.show()
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/image-2.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=3c1830bc53643155d2128a38a6a0fe70" alt="Image" width="803" height="585" data-path="images/image-2.png" />
</Frame>

***

# 5. Basic Charts

## A. Line Chart

```python theme={null}
import plotly.express as px

df = px.data.gapminder().query("country=='India'")

fig = px.line(df,
              x="year",
              y="lifeExp",
              title="Life Expectancy")

fig.show()
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/image-3.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=c89a8580cdee4841746016993bea7365" alt="Image" width="803" height="560" data-path="images/image-3.png" />
</Frame>

***

## B. Scatter Plot

```python theme={null}
fig = px.scatter(df,
                 x="gdpPercap",
                 y="lifeExp",
                 color="continent",
                 size="pop")
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/image-4.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=91d2c36e5a360d7394b066ff417c6558" alt="Image" width="812" height="567" data-path="images/image-4.png" />
</Frame>

***

## C. Bar Chart

```python theme={null}
fig = px.bar(df,
             x="year",
             y="pop"
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-barplot.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=e3852c0001656c5e67a840865fff4d84" alt="Plotly Barplot" width="796" height="567" data-path="images/plotly-barplot.png" />
</Frame>

***

## D. Histogram

```python theme={null}
fig = px.histogram(df,
                   x="lifeExp")
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-histogram.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=f7aaa2b405bc50870021898153de458e" alt="Plotly Histogram" width="812" height="567" data-path="images/plotly-histogram.png" />
</Frame>

***

## E. Box Plot

```python theme={null}
fig = px.box(df,
             x="continent",
             y="lifeExp")
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-boxplot1.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=0df97842f738a2a91316e0e61bf7bdf1" alt="Plotly Boxplot1" width="807" height="568" data-path="images/plotly-boxplot1.png" />
</Frame>

***

## F. Violin Plot

```python theme={null}
fig = px.violin(df,
                x="continent",
                y="lifeExp")
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-voilinplot.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=ddab23d29f0a7aa1eaacf6ceb9cdd5f1" alt="Plotly Voilinplot" width="802" height="562" data-path="images/plotly-voilinplot.png" />
</Frame>

***

## G. Pie Chart

```python theme={null}
fig = px.pie(df,
             names="continent",
             values="pop")
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-piechart.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=785871b195da910eaed8ecfbaa46d8e9" alt="Plotly Piechart" width="810" height="580" data-path="images/plotly-piechart.png" />
</Frame>

***

## H. Area Chart

```python theme={null}
fig = px.area(df,
              x="year",
              y="pop")
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-areachart.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=7d88304b188479fc78115f0eba91d334" alt="Plotly Areachart" width="813" height="565" data-path="images/plotly-areachart.png" />
</Frame>

***

# 6. Bubble Chart

```python theme={null}
fig = px.scatter(
    df,
    x="gdpPercap",
    y="lifeExp",
    size="pop",
    color="continent"
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-bubblechart.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=cf5ffac2312e6e3c05ecbd876cea65c0" alt="Plotly Bubblechart" width="817" height="572" data-path="images/plotly-bubblechart.png" />
</Frame>

***

# 7. Sunburst Chart

```python theme={null}
fig = px.sunburst(
    df,
    path=["continent", "country"],
    values="pop"
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-sunbrust.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=042dfcaf7f93d8e4d16134065716a22f" alt="Plotly Sunbrust" width="831" height="575" data-path="images/plotly-sunbrust.png" />
</Frame>

***

# 8. Treemap

```python theme={null}
fig = px.treemap(
    df,
    path=["continent", "country"],
    values="pop"
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-treemap.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=a7949015d077b878f673fa4fda9e5127" alt="Plotly Treemap" width="806" height="481" data-path="images/plotly-treemap.png" />
</Frame>

***

# 9. Funnel Chart

```python theme={null}
data = dict(
    stage=["Visit","Signup","Purchase"],
    users=[1000,500,200]
)

fig = px.funnel(
    data,
    x="users",
    y="stage"
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-funnelchart.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=ec308c3b7a7370a984403f0cfca859b1" alt="Plotly Funnelchart" width="817" height="565" data-path="images/plotly-funnelchart.png" />
</Frame>

***

# 10. Density Heatmap

```python theme={null}
fig = px.density_heatmap(
    df,
    x="gdpPercap",
    y="lifeExp"
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-density1.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=ab3e61a7b49837c33d5a09e74cac7de8" alt="Plotly Density1" width="822" height="576" data-path="images/plotly-density1.png" />
</Frame>

***

# 11. Density Contour

```python theme={null}
fig = px.density_contour(
    df,
    x="gdpPercap",
    y="lifeExp"
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-density2.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=2582f82b2d53cce55862080556882ec3" alt="Plotly Density2" width="813" height="556" data-path="images/plotly-density2.png" />
</Frame>

***

# 12. Choropleth Map

```python theme={null}
fig = px.choropleth(
    df,
    locations="iso_alpha",
    color="lifeExp"
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-cmap.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=9f621786a53907cb72f9f0d165d85536" alt="Plotly Cmap" width="803" height="573" data-path="images/plotly-cmap.png" />
</Frame>

***

# 13. Scatter Geo

```python theme={null}
fig = px.scatter_geo(
    df,
    locations="iso_alpha",
    size="pop"
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-scattergeo.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=027b8b1b9d04f1539f33aa575d736ef1" alt="Plotly Scattergeo" width="857" height="575" data-path="images/plotly-scattergeo.png" />
</Frame>

***

# 14. Timeline Chart

```python theme={null}
import pandas as pd

data = pd.DataFrame({
    "Task":["A","B"],
    "Start":["2024-01-01","2024-03-01"],
    "Finish":["2024-02-01","2024-05-01"]
})

fig = px.timeline(
    data,
    x_start="Start",
    x_end="Finish",
    y="Task"
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-timeline.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=0c59446f52f696492fcc5f0924c23ae9" alt="Plotly Timeline" width="857" height="587" data-path="images/plotly-timeline.png" />
</Frame>

***

# 15. Polar Chart

```python theme={null}
fig = px.line_polar(
    r=[1,2,3],
    theta=[0,90,180]
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-polar.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=82dc2a518d17e8500de6f3bf5f5c8b5b" alt="Plotly Polar" width="810" height="580" data-path="images/plotly-polar.png" />
</Frame>

***

# 16. Radar Chart

```python theme={null}
fig = go.Figure()

fig.add_trace(go.Scatterpolar(
    r=[5,4,3,4],
    theta=['Math','Physics','Chemistry','English'],
    fill='toself'
))

fig.show()
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-radar.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=a93eb2832adf097718cccbf79929da17" alt="Plotly Radar" width="810" height="570" data-path="images/plotly-radar.png" />
</Frame>

***

# 17. 3D Scatter Plot

```python theme={null}
fig = px.scatter_3d(
    df,
    x="gdpPercap",
    y="lifeExp",
    z="pop"
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-3dscatter.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=3a5add0b91c5f2cfe5f2377c0b5b99c2" alt="Plotly 3dscatter" width="807" height="560" data-path="images/plotly-3dscatter.png" />
</Frame>

***

# 18. 3D Line Plot

```python theme={null}
fig = px.line_3d(
    df,
    x="year",
    y="lifeExp",
    z="pop"
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-3dline.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=6ac48ac036c1cef7a464ee75572c0d38" alt="Plotly 3dline" width="795" height="565" data-path="images/plotly-3dline.png" />
</Frame>

***

# 19. Surface Plot

```python theme={null}
import numpy as np

z = np.random.rand(10,10)

fig = go.Figure(
    data=[go.Surface(z=z)]
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-surface.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=009f17f092cb616cf31c72dee28ac455" alt="Plotly Surface" width="798" height="566" data-path="images/plotly-surface.png" />
</Frame>

***

# 20. Mesh3D

```python theme={null}
fig = go.Figure(
    data=[go.Mesh3d(
        x=[0,1,2],
        y=[0,1,0],
        z=[0,1,2]
    )]
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-mesh3d.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=b8ef798036e67e44b3acd59e419d4de1" alt="Plotly Mesh3d" width="810" height="572" data-path="images/plotly-mesh3d.png" />
</Frame>

***

# 21. Graph Objects

Graph Objects provide full control.

```python theme={null}
import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(
    go.Bar(
        x=["A","B","C"],
        y=[10,20,30]
    )
)

fig.show()
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-go.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=141bb5964ea8d9bf7fd7dd4d349f96b3" alt="Plotly Go" width="802" height="558" data-path="images/plotly-go.png" />
</Frame>

***

# 22. Figure Layout

```python theme={null}
fig.update_layout(
    title="Sales Report",
    xaxis_title="Month",
    yaxis_title="Sales",
    template="plotly_dark"
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-go2.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=6208192cb5eaa3b772808076217b5874" alt="Plotly Go2" width="735" height="531" data-path="images/plotly-go2.png" />
</Frame>

***

# 23. Templates

```python theme={null}
template="plotly"

template="plotly_dark"

template="ggplot2"

template="seaborn"

template="simple_white"
```

***

# 24. Multiple Traces

```python theme={null}
fig = go.Figure()

fig.add_trace(
    go.Scatter(
        x=[1,2,3],
        y=[4,5,6]
    )
)

fig.add_trace(
    go.Scatter(
        x=[1,2,3],
        y=[6,5,4]
    )
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-multitrace.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=74dcf95fe7115133a94eda65b927840c" alt="Plotly Multitrace" width="811" height="552" data-path="images/plotly-multitrace.png" />
</Frame>

***

# 25. Subplots

```python theme={null}
from plotly.subplots import make_subplots

fig = make_subplots(
    rows=1,
    cols=2
)

fig.add_trace(
    go.Bar(
        x=[1,2],
        y=[3,4]
    ),
    row=1,
    col=1
)

fig.show()
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-subplot.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=1a67ea8e1f23206f87ff90e0b656510b" alt="Plotly Subplot" width="830" height="570" data-path="images/plotly-subplot.png" />
</Frame>

***

# 26. Update Traces

```python theme={null}
fig.update_traces(
    marker_color="red",
    marker_size=12
)
```

***

# 27. Update Axes

```python theme={null}
fig.update_xaxes(title="Year")

fig.update_yaxes(title="Population")
```

***

# 28. Hover Information

```python theme={null}
fig.update_traces(
    hovertemplate="%{x}<br>%{y}"
)
```

***

# 29. Annotations

```python theme={null}
fig.add_annotation(
    x=2,
    y=5,
    text="Peak"
)
```

***

# 30. Shapes

Rectangle:

```python theme={null}
fig.add_shape(
    type="rect",
    x0=1,
    y0=2,
    x1=3,
    y1=5
)
```

Line:

```python theme={null}
fig.add_shape(
    type="line",
    x0=0,
    y0=0,
    x1=5,
    y1=5
)
```

***

# 31. Animation

```python theme={null}
fig = px.scatter(
    df,
    x="gdpPercap",
    y="lifeExp",
    animation_frame="year"
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/iOF-Ay7b9DslVIJF/images/plotly-animation.png?fit=max&auto=format&n=iOF-Ay7b9DslVIJF&q=85&s=7734e31449146fa436968866cf7ff157" alt="Plotly Animation" width="831" height="592" data-path="images/plotly-animation.png" />
</Frame>

***

# 32. Export Graph

HTML:

```python theme={null}
fig.write_html("graph.html")
```

PNG:

```python theme={null}
fig.write_image("graph.png")
```

(Requires the `kaleido` package.)

***

# 33. Display Figure

```python theme={null}
fig.show()
```

***

# 34. Common Plotly Express Parameters

| Parameter        | Description           |
| ---------------- | --------------------- |
| data\_frame      | Input dataset         |
| x                | X-axis column         |
| y                | Y-axis column         |
| color            | Color grouping        |
| size             | Bubble size           |
| symbol           | Marker shape          |
| title            | Chart title           |
| labels           | Axis labels           |
| hover\_name      | Main hover text       |
| hover\_data      | Additional hover data |
| facet\_row       | Row-wise facets       |
| facet\_col       | Column-wise facets    |
| animation\_frame | Animation step        |
| animation\_group | Object tracking       |
| template         | Theme                 |
| width            | Figure width          |
| height           | Figure height         |

***

# 35. Common Graph Objects

* `go.Scatter`
* `go.Bar`
* `go.Pie`
* `go.Box`
* `go.Violin`
* `go.Histogram`
* `go.Heatmap`
* `go.Surface`
* `go.Mesh3d`
* `go.Scatter3d`
* `go.Scatterpolar`
* `go.Indicator`
* `go.Table`
* `go.Funnel`

***

# 36. Advantages of Plotly

* Interactive visualizations
* Browser-based rendering
* Publication-quality graphics
* Supports 2D and 3D charts
* Easy integration with Dash
* Highly customizable
* Animation support
* Hover tooltips and zooming
* Export to HTML and images

***

# 37. Limitations

* Can be slower with very large datasets
* More memory-intensive than static libraries
* Some advanced exports require additional packages (such as `kaleido`)

***

# 38. Plotly vs Matplotlib

| Feature            | Plotly           | Matplotlib     |
| ------------------ | ---------------- | -------------- |
| Interactive        | ✅                | ❌ (by default) |
| 3D Support         | ✅                | ✅              |
| Animation          | ✅                | ✅              |
| Web Integration    | Excellent        | Limited        |
| Learning Curve     | Easy to Moderate | Moderate       |
| Static Publication | Good             | Excellent      |
| Dashboards         | Excellent        | Limited        |

***

# 39. Summary

* **Plotly Express (`px`)** is ideal for quickly creating interactive charts.
* **Graph Objects (`go`)** provide fine-grained control and customization.
* **Key chart types:** Line, Scatter, Bar, Pie, Histogram, Box, Violin, Heatmap, Treemap, Sunburst, Funnel, Timeline, Polar, 3D Scatter, Surface, Mesh3D, and Geographic Maps.
* **Core workflow:** Create a `Figure`, add traces (if using `go`), customize with `update_layout()`, `update_traces()`, and `update_xaxes()/update_yaxes()`, then display with `fig.show()` or export with `write_html()`/`write_image()`.
