Adjusting marker sizes and colors

In a scatter plot, we can specify the marker size with the parameter s and the marker color with c in the plt.scatter() function.

To draw markers on line plots, we first specify the shape of the markers in the plt.plot() function, such as marker='x'. Marker colors follow the line color.

Please note that scatter plots accept list types as size and color values, convenient in visualizing clusters, while line plots only accept a single value per data series.

Let's look at the following example:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors

# Prepare a list of integers
n = list(range(5))

# Prepare a list of sizes that increases with values in n
s = [i**2*100+100 for i in n]

# Prepare a list of colors
c = ['red','orange','yellow','green','blue']

# Draw a scatter plot of n points with sizes in s and colors in c
plt.scatter(n,n,s=s,c=c)

# Draw a line plot with n points with black cross markers of size 12
plt.plot(n,marker='x',color='black',ms=12)

# Set axis limits to show the markers completely
plt.xlim(-0.5,4.5)
plt.ylim(-1,5)

plt.show()

This code generates a figure of a scatter plot with marker sizes increasing with the data values, and a line plot with cross-shaped markers of a fixed size:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.201.75