Matplotlib Scatter Marker Size

Matplotlib Scatter Marker Size

Matplotlib is a popular plotting library in Python that allows users to create a wide range of visualizations, including scatter plots. One of the many customization options available in Matplotlib scatter plots is the ability to adjust the marker size. In this tutorial, we will explore how to customize the marker size in scatter plots using Matplotlib.

Basic Scatter Plot with Default Marker Size

Let’s start by creating a basic scatter plot with default marker size. We will use sample data to demonstrate this.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]

plt.scatter(x, y)
plt.title('Basic Scatter Plot')
plt.show()

Output:

Matplotlib Scatter Marker Size

In the above example, we have created a simple scatter plot with default marker size. The markers are displayed with their default size in the plot.

Customizing Marker Size in Scatter Plot

To customize the marker size in a scatter plot, we can use the s parameter in the plt.scatter() function. The s parameter allows us to specify the marker size in points.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
sizes = [20, 50, 80, 200, 500]

plt.scatter(x, y, s=sizes)
plt.title('Customized Marker Size Scatter Plot')
plt.show()

Output:

Matplotlib Scatter Marker Size

In the above example, we have customized the marker size by specifying a list of sizes for each marker. The markers are displayed with different sizes based on the values in the sizes list.

Using the c Parameter for Marker Color

In addition to changing the marker size, we can also customize the marker color in a scatter plot using the c parameter.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
sizes = [20, 50, 80, 200, 500]
colors = ['red', 'green', 'blue', 'yellow', 'purple']

plt.scatter(x, y, s=sizes, c=colors)
plt.title('Customized Marker Size and Color Scatter Plot')
plt.show()

Output:

Matplotlib Scatter Marker Size

In the above example, we have customized both the marker size and color by specifying lists of sizes and colors for each marker. The markers are displayed with different sizes and colors based on the values in the sizes and colors lists.

Changing Marker Size Based on Data

We can also change the marker size dynamically based on the data value. For example, we can scale the marker size based on the values in the sizes list.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
sizes = [20, 50, 80, 200, 500]

plt.scatter(x, y, s=sizes, c='blue')
plt.title('Dynamic Marker Size Scatter Plot')
plt.show()

Output:

Matplotlib Scatter Marker Size

In the above example, we have scaled the marker size based on the values in the sizes list. The markers are displayed with different sizes based on the values in the sizes list.

Adjusting Marker Size Range

We can also adjust the range of marker sizes by specifying the vmin and vmax parameters in the plt.scatter() function.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
sizes = [20, 50, 80, 200, 500]

plt.scatter(x, y, s=sizes, c='green', vmin=0, vmax=500)
plt.title('Adjusting Marker Size Range Scatter Plot')
plt.show()

In the above example, we have adjusted the range of marker sizes by specifying vmin=0 and vmax=500. The markers are displayed with sizes scaled between 0 and 500 based on the values in the sizes list.

Using Colormap for Marker Size

We can use a colormap to map marker sizes to a specific color scheme. This can be done by specifying the cmap parameter in the plt.scatter() function.

import numpy as np
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
sizes = [20, 50, 80, 200, 500]

plt.scatter(x, y, s=sizes, c=sizes, cmap='viridis')
plt.colorbar()
plt.title('Using Colormap for Marker Size Scatter Plot')
plt.show()

Output:

Matplotlib Scatter Marker Size

In the above example, we have used the ‘viridis’ colormap to map marker sizes to a specific color scheme. The markers are displayed with sizes and colors based on the values in the sizes list.

Highlighting Specific Data Points

We can highlight specific data points in a scatter plot by adjusting the marker size for those points. For example, we can increase the size of data points that meet a specific condition.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
sizes = [20, 50, 80, 200, 500]
highlight = [True, False, True, False, True]

plt.scatter(x, y, s=[size*2 if highlight else size for size, highlight in zip(sizes, highlight)], c='orange')
plt.title('Highlighting Specific Data Points Scatter Plot')
plt.show()

Output:

Matplotlib Scatter Marker Size

In the above example, we have increased the size of data points that meet the condition specified in the highlight list. The highlighted data points are displayed with larger markers.

Adjusting Marker Size Based on Data Range

We can adjust the marker size based on the range of data values. For example, we can scale the marker size based on the min-max normalization of the data values.

import matplotlib.pyplot as plt
import numpy as np

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
data = [10, 20, 30, 40, 50]

sizes = [(d-min(data))/(max(data)-min(data))*100 for d in data]

plt.scatter(x, y, s=sizes, c='purple')
plt.title('Adjusting Marker Size Based on Data Range Scatter Plot')
plt.show()

Output:

Matplotlib Scatter Marker Size

In the above example, we have scaled the marker size based on the min-max normalization of the data values in the data list. The markers are displayed with sizes scaled between 0 and 100 based on the data values.

Using Marker Size as a Weight

We can also use the marker size as a weight to represent additional information in the scatter plot. For example, we can use the marker size to represent the magnitude of a third variable.

import matplotlib.pyplot as plt
import numpy as np

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
data = [10, 20, 30, 40, 50]

sizes = [d*5 for d in data]

plt.scatter(x, y, s=sizes, c='pink')
plt.title('Using Marker Size as a Weight Scatter Plot')
plt.show()

Output:

Matplotlib Scatter Marker Size

In the above example, we have used the marker size to represent the magnitude of the data values in the data list. The markers are displayed with sizes scaled based on the data values to represent additional information.

Matplotlib Scatter Marker Size Conclusion

In this tutorial, we have explored how to customize the marker size in scatter plots using Matplotlib. We have demonstrated various techniques for adjusting the marker size based on data values, using different colors and colormaps, highlighting specific data points, and using the marker size as a weight to represent additional information. By customizing the marker size in scatter plots, we can create visually appealing and informative visualizations for our data.

Like(0)