scatter plot plotly python code example

Example 1: how to plot a scatter plot in matplotlib

# Import matplotlib
import matplotlib.pyplot as plt

# Set plot space as inline for inline plots and qt for external plots
%matplotlib inline


# Set the figure size in inches
plt.figure(figsize=(10,6))

plt.scatter(x, y, label = "label_name" )

# Set x and y axes labels
plt.xlabel('X Values')
plt.ylabel('Y Values')

plt.title('Scatter Title')
plt.legend()
plt.show()

Example 2: go.scatter

import plotly.graph_objects as go

fig = go.Figure()

# Add traces
fig.add_trace(go.Scatter(x=df['col_x'], y=df['col_y'],
                    mode='markers',
                    name='markers'))
fig.add_trace(go.Scatter(x=df2['col_x'], y=df2['col_y'],
                    mode='lines+markers',
                    name='lines+markers'))

fig.show()

Example 3: scatter plot plotly

import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
                 size='petal_length', hover_data=['petal_width'])
fig.show()

Example 4: matplotlib scatter plot python

import numpy as np
np.random.seed(19680801)
import matplotlib.pyplot as plt


fig, ax = plt.subplots()
for color in ['tab:blue', 'tab:orange', 'tab:green']:
    n = 750
    x, y = np.random.rand(2, n)
    scale = 200.0 * np.random.rand(n)
    ax.scatter(x, y, c=color, s=scale, label=color,
               alpha=0.3, edgecolors='none')

ax.legend()
ax.grid(True)

plt.show()