matplotlib. pyplot. Scattergram

Posted by thipse_rahul on Thu, 03 Mar 2022 07:32:32 +0100

usage

matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data=None, **kwargs)

Parameter introduction

x,yData for plotting scatter plotsfloat or array-like, shape(n,)
sA real number or an array with the size of (n,), an optional parameter, refers to the size of the scatter centerflaot or array-like, shape(n,)
cScatter colorarray-like or list of colors or color
markerThe default is' o '
cmapColormap instance or registered colormap name. Use cmap only if c is a floating-point array
normIt is also a color data. Use norm to scale the color data c in the range of 0 to 1 and map it to colormap cmap. If not, use the default color and normalize itdefault:None
vmin,vmaxvmin and vmax are used with the default norm to map the color array c to the colormap cmap. If not, the minimum and maximum values of the color array are used respectively. It is wrong to use vmin/vmax when a norm is givenfloat, default:None
alphaTransparency, numbers between 0,1float,default: None
linewidthsLine weight of the marked edgefloat or array-like,default: 1.5
edgecolorsScatter edge line color'color of face' is not the same as' color of none '
plotnonfiniteWhether points are drawn using non finite c (i.e., inf, - inf, or nan). If True, points are drawn with the wrong colormap color

There are two parameters that I put together and use code to interpret c,edgecolors at the same time
Parameters c and edgecolors, linewidths

import matplotlib.pyplot as plt
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
# To show c and edgecolors, I increased the linewidths to 15
plt.scatter(a, b, linewidths=15,c='red',edgecolors=['black', 'green','cyan','lightgreen'])


Parameter s

import matplotlib.pyplot as plt
%matplotlib inline

plt.figure(figsize=(12,6))
plt.rcParams['font.family'] = 'SimHei'

a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
plt.subplot(121)
plt.title('Do not add s')
plt.scatter(a,b,c='red',linewidths=6)

plt.subplot(122)
plt.title('add to s,s=1.5')
plt.scatter(a,b,c='red',s=1.5,linewidths=6)
plt.show()


Parameter marker

import matplotlib.pyplot as plt

plt.figure(figsize=(12,6))
plt.rcParams['font.family'] = 'SimHei'

a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
plt.subplot(131)
plt.title('standard')
plt.scatter(a,b,linewidths=6)

plt.subplot(132)
plt.title("set up marker 'x'")
plt.scatter(a,b,marker='x')

plt.subplot(133)
plt.title("set up marker 'v'")
plt.scatter(a,b,marker='v')
plt.show()


marker attribute

markerdescription
.spot
,Pixels, similar to squares
oCircle, default
vInverted triangle
^Regular triangle
<Left triangle
>Right triangle
1tri_down
2tri_up
3tri_left
4tri_right
8Octagonal
ssquare
pPentagonal
*stars
hHex 1
HHexagon 2
+plus
xx number
DDiamonds
dFine drill
"l" - > a vertical linev line
_H line

So many updates first, which will be introduced later
Here, maker=1 or 2 or 3 or 4 is displayed

import matplotlib.pyplot as plt

plt.figure(figsize=(12,6))
plt.rcParams['font.family'] = 'SimHei'

a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]

plt.subplot(141)
plt.title("set up marker '1'")
plt.scatter(a,b,s=100,marker='1')

plt.subplot(142)
plt.title("set up marker '2'")
plt.scatter(a,b,s=100,marker='2')

plt.subplot(143)
plt.title("set up marker '3'")
plt.scatter(a,b,s=100,marker='3')

plt.subplot(144)
plt.title("set up marker '4'")
plt.scatter(a,b,s=100,marker='4')

plt.show()


Parameter cmap
cmap is mainly used together with c parameter. c can be a color sequence, which is replaced by a number list
plt.cm.Spectral is a color mapping set, which does not mean that [0:5] represents a certain color. There are five different values for parameter c
Then assign a color to each value

import matplotlib.pyplot as plt

a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
c = [0, 1, 2, 3, 4]
plt.rcParams['font.family'] = 'SimHei'

plt.subplot(121)
plt.title('standard')
plt.scatter(a, b, c=c, s=80)

plt.subplot(122)
plt.title('add to cmap')
plt.scatter(a, b, c=c, s=80, cmap=plt.cm.Spectral)

plt.show()


vmin,vmax,norm, scatter brightness setting, alpha transparency
plt. Colorbar
The scatter chart is set to be a bubble chart, which is shown below

import matplotlib.pyplot as plt
# Import color bar library
from matplotlib import colors
import numpy as np

x = np.random.randn(50) # Randomly generate 50 X coordinates
y = np.random.randn(50) # Randomly generate 50 Y coordinates
color = np.random.rand(50) # Randomly generate values for mapping colors
size = 500 * np.random.rand(50) # Values that randomly change the size of scatter points
changecolor = colors.Normalize(vmin=0.4, vmax=0.8)
plt.scatter(x, y, c=color, s = size, alpha=0.3, cmap='viridis', norm=changecolor)
plt.colorbar() # Show color bar
plt.show()

Topics: Python Data Analysis