Python study notes (h) - (3) matplotlib two-dimensional and three-dimensional mapping of the scatter plot

1 Introduction

Scatter plot is relatively common, with painted python results were pretty good, but more flexible regulation.

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

x, y : array_like, shape (n, )

Input data

s : scalar or array_like, shape (n, ), optional

size in points^2. Default is rcParams['lines.markersize'] ** 2.

c : color, sequence, or sequence of color, optional, default: ‘b’

c can be a single color format string, or a sequence of color specifications of length N, or a sequence of N numbers to be mapped to colors using the cmap and norm specified via kwargs (see below). Note that c should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped. c can be a 2-D array in which the rows are RGB or RGBA, however, including the case of a single row to specify the same color for all points.

marker : MarkerStyle, optional, default: ‘o’

See markers for more information on the different styles of markers scatter supports. marker can be either an instance of the class or the text shorthand for a particular marker.

cmap : Colormap, optional, default: None

A Colormap instance or registered name. cmap is only used if c is an array of floats. If None, defaults to rc image.cmap.

norm : Normalize, optional, default: None

A Normalize instance is used to scale luminance data to 0, 1. norm is only used if c is an array of floats. If None, use the default normalize().

vmin, vmax : scalar, optional, default: None

vmin and vmax are used in conjunction with norm to normalize luminance data. If either are None, the min and max of the color array is used. Note if you pass a norm instance, your settings for vmin and vmax will be ignored.

alpha : scalar, optional, default: None

The alpha blending value, between 0 (transparent) and 1 (opaque)

linewidths : scalar or array_like, optional, default: None

If None, defaults to (lines.linewidth,).

verts : sequence of (x, y), optional

If marker is None, these vertices will be used to construct the marker. The center of the marker is located at (0,0) in normalized units. The overall marker is rescaled by s.

edgecolors : color or sequence of color, optional, default: None

If None, defaults to ‘face’

If ‘face’, the edge color will always be the same as the face color.

If it is ‘none’, the patch boundary will not be drawn.

For non-filled markers, the edgecolors kwarg is ignored and forced to ‘face’ internally.


2. The two-dimensional scatter

Code:

# coding=gbk
import numpy as np
import matplotlib.pyplot as plt               
x = np.linspace(-np.pi,np.pi,50,endpoint=True)     # 最末尾的点也画上
y = np.cos(x)
#--------------------------scatter-------------------------------#
# help(plt.scatter)
plt.figure()
S = [i for i in range(5,40)]  # SizeData,自动循环
ColorBase = ('r','g','b','c','k','m','y')  # 颜色,自动循环
Marker = 'o'
MarkerBase = (',','o','v','^','<','>','1','2','3','4','8',
              's','p','*','h','+','x','D','d','|','_'
              'TICKLEFT','TICKRIGHT','TICKUP','TICKDOWN',
              'CARETLEFT','CARETRIGHT','CARETUP','CARETDOWN')  # 向量形式
Linewidths = [1,2] # 向量形式,自动循环
EdgeColor = ('k','k','r','r','g','g')
plt.scatter(x,y,s=S,c=ColorBase,marker=Marker,linewidths=Linewidths,edgecolors=EdgeColor) 
plt.savefig('2-D.jpg',fmt='jpg')

2-D.jpg:


The three-dimensional scatter

Code:
#--------------------------scatter 3-------------------------------#
from mpl_toolkits.mplot3d import Axes3D  # 三维作图      
# Axes3D.scatter(xs, ys, zs=0, zdir='z', s=20, c=None, depthshade=True, *args, **kwargs)
ax = Axes3D(plt.figure())
NumP = 50
x = np.random.rand(NumP)
y = np.random.rand(NumP)
z = np.random.rand(NumP)
#-----画一个平行于xy的平面-----#
ax.scatter(x,y,5,s=40,c='r',edgecolor='k',alpha=0.5)
plt.savefig('3-D-1.jpg',fmt='jpg')

#-------改变大小和颜色--------#
ax = Axes3D(plt.figure())  # 重新创建一个
S = [i for i in range(1,NumP+1)]  # It is a scalar or an array of the same length as x and y.
def GetColor(N):
    NumColor = len(ColorBase)
    Color = [ColorBase[0]]
    Iter = 1
    for i in range(N-1):
        if Iter>=NumColor:
            Iter = 0
        Color.append(ColorBase[Iter])
        Iter = Iter+1
    return(Color)
Color = GetColor(NumP)
print(len(Color),Color)  # c can be a single color format string;
# or a sequence of color specifications of length N;
# or a sequence of N numbers to be mapped to colors using the cmap and norm specified via kwargs (see below). 
# Note that c should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped. 
# c can be a 2-D array in which the rows are RGB or RGBA, however, including the case of a single row to specify the same color for all points.
ax.scatter(x,y,z,s=S,c=Color,marker=Marker,edgecolors='k')
plt.savefig('3-D-2.jpg',fmt='jpg')

#-------------------------图像显示---------------------------------#
plt.show()  # 显示

3-D-1.jpg:


3-D-2.jpg:



Output:

50 ['r', 'g', 'b', 'c', 'k', 'm', 'y', 'r', 'g', 'b', 'c', 'k', 'm', 'y', 'r', 'g', 'b', 'c', 'k', 'm', 'y', 'r', 'g', 'b', 'c', 'k', 'm', 'y', 'r', 'g', 'b', 'c', 'k', 'm', 'y', 'r', 'g', 'b', 'c', 'k', 'm', 'y', 'r', 'g', 'b', 'c', 'k', 'm', 'y', 'r']

4. Summary


  • (1) common two-dimensional adjustment scatter: marker size, type, color, line thickness;
  • (2) uses a three-dimensional Axes3D class, similar to the two-dimensional, but different, such as input sizes, dimensions and the color vector must be the same number of points, can program experience


Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed.

Published 14 original articles · won praise 19 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_24694761/article/details/79201039