Home > Backend Development > Python Tutorial > How Does Matplotlib\'s `pyplot.scatter()` Function Use the `s` Parameter to Control Marker Size?

How Does Matplotlib\'s `pyplot.scatter()` Function Use the `s` Parameter to Control Marker Size?

Linda Hamilton
Release: 2024-11-25 15:38:12
Original
266 people have browsed it

How Does Matplotlib's `pyplot.scatter()` Function Use the `s` Parameter to Control Marker Size?

pyplot Scatter Plot Marker Size

In the matplotlib.pyplot.scatter() function, the s parameter specifies the marker size. This size is defined in "points^2," which can be a confusing unit of measurement to interpret.

What is a "Point"?

A "point" in this context is an arbitrary unit of measurement used for defining the size of markers. It is not directly related to the size of a pixel on your display.

How Does s Affect Marker Size?

The s parameter specifies the area of the marker. This means that:

  • Increasing s by a factor of 4 increases the width and height of the marker by a factor of 2.
  • Doubling the width of the marker (or any linear dimension) requires increasing s by a factor of 4.
  • Doubling the area of the marker requires increasing s by a factor of 2.

Example

Let's create a scatter plot with different marker sizes:

import matplotlib.pyplot as plt

x = [0, 2, 4, 6, 8, 10]
y = [0] * len(x)
s = [20 * 4**n for n in range(len(x))]

plt.scatter(x, y, s=s)
plt.show()
Copy after login

In this example, the marker size increases exponentially as we move from left to right. Each marker has twice the area of the previous marker.

Visualizing Marker Size

To visualize the different functions that affect marker size, let's create the following plot:

import matplotlib.pyplot as plt

x = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
s_exp = [20 * 2**n for n in range(len(x))]
s_square = [20 * n**2 for n in range(len(x))]
s_linear = [20 * n for n in range(len(x))]

plt.scatter(x, [1] * len(x), s=s_exp, label='$s=2^n$', lw=1)
plt.scatter(x, [0] * len(x), s=s_square, label='$s=n^2$')
plt.scatter(x, [-1] * len(x), s=s_linear, label='$s=n$')
plt.ylim(-1.5, 1.5)
plt.legend(loc='center left', bbox_to_anchor=(1.1, 0.5), labelspacing=3)
plt.show()
Copy after login

This plot demonstrates how the marker size appears when scaled exponentially, quadratically, and linearly.

The above is the detailed content of How Does Matplotlib\'s `pyplot.scatter()` Function Use the `s` Parameter to Control Marker Size?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template