Matplotlib Scatter

Matplotlib Scatter

Matplotlib is a popular plotting library in Python that provides a wide range of functions to create different types of visualizations. One of the most commonly used plotting functions is scatter, which is used to create scatter plots. In this article, we will explore the scatter function in detail and learn how to use it effectively to visualize data.

What is a Matplotlib Scatter Plot?

A scatter plot is a two-dimensional graph that displays individual data points as markers or dots. Each point represents the values of two different variables, with one variable plotted on the x-axis and the other on the y-axis. Scatter plots are useful for identifying relationships or patterns between two variables and for visualizing the distribution of data.

Syntax of Matplotlib Scatter:

The syntax of the scatter function in Matplotlib is as follows:

plt.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, edgecolors=None)

Let’s discuss the important parameters of the scatter function:

  • x and y: The coordinates of the data points on the x and y-axis.
  • s: The size of the markers, either a scalar or an array of the same length as x and y.
  • c: The color of the markers. It can be a single color or a sequence of colors corresponding to each data point.
  • marker: The marker style for the data points.
  • cmap: A colormap to use for coloring the markers.
  • alpha: The transparency of the markers.
  • linewidths: The width of the marker edges.
  • edgecolors: The color of the marker edges.

Matplotlib Scatter Code Examples:

Let’s now look at some code examples to understand how to use the scatter function effectively. We will plot different types of scatter plots with different customization options.

  • Example 1: Basic Scatter Plot
import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Scatter plot
plt.scatter(x, y)

# Show plot
plt.show()

Output:
Matplotlib Scatter

  • Example 2: Scatter Plot with Marker Size
import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
sizes = [10, 20, 30, 40, 50]

# Scatter plot with marker size
plt.scatter(x, y, s=sizes)

# Show plot
plt.show()

Output:
Matplotlib Scatter

  • Example 3: Scatter Plot with Marker Color
import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
colors = ['red', 'green', 'blue', 'yellow', 'orange']

# Scatter plot with marker color
plt.scatter(x, y, c=colors)

# Show plot
plt.show()

Output:
Matplotlib Scatter

  • Example 4: Scatter Plot with Marker Style
import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Scatter plot with marker style
plt.scatter(x, y, marker='o')

# Show plot
plt.show()

Output:
Matplotlib Scatter

  • Example 5: Scatter Plot with Colormap
import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.random.rand(100)
y = np.random.rand(100)
colors = np.random.rand(100)

# Scatter plot with colormap
plt.scatter(x, y, c=colors, cmap='viridis')

# Show plot
plt.show()

Output:
Matplotlib Scatter

  • Example 6: Scatter Plot with Transparency
import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Scatter plot with transparency
plt.scatter(x, y, alpha=0.5)

# Show plot
plt.show()

Output:
Matplotlib Scatter

  • Example 7: Scatter Plot with Marker Edges
import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Scatter plot with marker edges
plt.scatter(x, y, edgecolors='black')

# Show plot
plt.show()

Output:
Matplotlib Scatter

  • Example 8: Scatter Plot with Colorbar
import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.random.rand(100)
y = np.random.rand(100)
colors = np.random.rand(100)

# Scatter plot with colorbar
scatter = plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar(scatter)

# Show plot
plt.show()

Output:
Matplotlib Scatter

  • Example 9: Scatter Plot with Different Marker Sizes and Colors
import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.random.rand(100)
y = np.random.rand(100)
sizes = np.random.randint(10, 100, 100)
colors = np.random.rand(100)

# Scatter plot with marker sizes and colors
plt.scatter(x, y, c=colors, s=sizes)

# Show plot
plt.show()

Output:
Matplotlib Scatter

  • Example 10: Scatter Plot with Logarithmic Axes
import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.logspace(1, 10, 1000, base=10)
y = np.logspace(1, 10, 1000, base=2)

# Scatter plot with logarithmic axes
plt.scatter(x, y)

# Set logarithmic scale for both axes
plt.xscale('log')
plt.yscale('log')

# Show plot
plt.show()

Output:
Matplotlib Scatter

Matplotlib Scatter Conclusion

In this article, we explored the scatter function in Matplotlib and learned how to create various types of scatter plots. We discussed different parameters of the scatter function and saw code examples showcasing customizations like marker size, marker color, marker style, colormap, transparency, marker edges, colorbar, and logarithmic axes. By leveraging the scatter function effectively, you can visualize your data in an impactful and visually appealing manner.

Like(3)