Scatter Plots in Plotly

Scatter plots are essential for visualizing the relationship between two variables. This tutorial will guide you through various ways to create and customize scatter plots using the `plotly` library.

### Basic Scatter Plot

We'll start with a simple scatter plot using Plotly. First, let's import the necessary libraries and create a basic scatter plot.

import plotly.graph_objs as go
import plotly.express as px

# Sample data
x = [0, 1, 2, 3, 4, 5]
y = [0, 1, 4, 9, 16, 25]

# Create a Scatter plot
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='markers'))

fig.update_layout(
    title='Basic Scatter Plot',
    xaxis_title='X-axis',
    yaxis_title='Y-axis'
)

fig.show()
- **`go.Figure(data=go.Scatter(x=x, y=y, mode='markers'))`** creates a scatter plot with the given x and y data points.
- **`mode='markers'`** specifies that only markers (points) should be plotted.

### Adding Marker Colors and Sizes

You can customize the markers by changing their colors and sizes to make your scatter plot more informative.

import plotly.graph_objs as go
import plotly.express as px

# Sample data
x = [0, 1, 2, 3, 4, 5]
y = [0, 1, 4, 9, 16, 25]
sizes = [10, 20, 30, 40, 50, 60]
colors = ['red', 'green', 'blue', 'purple', 'orange', 'brown']

# Create a Scatter plot with custom markers
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='markers', 
                                marker=dict(size=sizes, color=colors)))

fig.update_layout(
    title='Scatter Plot with Custom Marker Sizes and Colors',
    xaxis_title='X-axis',
    yaxis_title='Y-axis'
)

fig.show()
- **`marker=dict(size=sizes, color=colors)`** customizes the size and color of the markers.

### Adding Text Labels

You can add text labels to each marker to provide additional information about the points.

import plotly.graph_objs as go
import plotly.express as px

# Sample data
x = [0, 1, 2, 3, 4, 5]
y = [0, 1, 4, 9, 16, 25]
text_labels = ['A', 'B', 'C', 'D', 'E', 'F']

# Create a Scatter plot with text labels
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='markers+text', 
                                text=text_labels, textposition='top center', 
                                marker=dict(size=15, color='blue')))

fig.update_layout(
    title='Scatter Plot with Text Labels',
    xaxis_title='X-axis',
    yaxis_title='Y-axis'
)

fig.show()
- **`mode='markers+text'`** adds text labels to the markers.
- **`text=text_labels`** specifies the text labels for each marker.
- **`textposition='top center'`** sets the position of the text labels.

### Scatter Plot with Different Modes

Plotly allows you to create scatter plots with different modes such as lines, markers, and lines+markers.

import plotly.graph_objs as go
import plotly.express as px

# Sample data
x = [0, 1, 2, 3, 4, 5]
y1 = [0, 1, 2, 3, 4, 5]
y2 = [0, 1, 4, 9, 16, 25]
y3 = [0, 1, 8, 27, 64, 125]

# Create a Scatter plot with lines and markers
fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=y1, mode='lines', name='Lines'))
fig.add_trace(go.Scatter(x=x, y=y2, mode='markers', name='Markers'))
fig.add_trace(go.Scatter(x=x, y=y3, mode='lines+markers', name='Lines+Markers'))

fig.update_layout(
    title='Scatter Plot with Different Modes',
    xaxis_title='X-axis',
    yaxis_title='Y-axis'
)

fig.show()
- **`mode='lines'`** creates a line plot.
- **`mode='lines+markers'`** creates a plot with both lines and markers.

### Scatter Plot with DataFrames

Plotly Express allows for quick scatter plots from data stored in pandas DataFrames.

import plotly.express as px
import pandas as pd

iris = px.data.iris()

# Create a Scatter plot from DataFrame
fig = px.scatter(iris, x='petal_length', y='petal_width', size='sepal_length',
                color='species', 
                title='Scatter Plot from DataFrame')

fig.update_traces(textposition='top center')

fig.show()
This creates a scatter plot directly from a DataFrame, where:
- **`x='x'`** maps the x-axis to the 'x' column in the DataFrame.
- **`y='y'`** maps the y-axis to the 'y' column in the DataFrame.
- **`size='sepal_length'`** determines the size of the markers based on the 'sepal_length' column.
- **`color='species'`** sets the marker colors based on the 'species' column.

### Example: Interactive Scatter Plot with Hover Information

In Plotly, you can add hover information to provide more context when users hover over points.

import plotly.express as px
import pandas as pd

iris = px.data.iris()

# Create a Scatter plot from DataFrame
fig = px.scatter(iris, x='petal_length', y='petal_width',
                color='species', 
                title='Scatter Plot from DataFrame', 
                opacity=0.5,
                hover_name='species',
                hover_data={'sepal_width': True, 'sepal_length': True})

fig.update_traces(textposition='top center', marker=dict(size=16), )

fig.show()
- **`hover_name='species'`** enables displaying the 'species' column on hover.
- **`hover_data={'sepal_width': True, 'sepal_length': True}`** shows data from "sepal_width" and "sepal_length" columns on hover.