Pythonの研究ノート(H) - 散布図の(3)matplotlibの二次元および三次元マッピング

1.はじめに

散布図は、塗装のpythonの結果はかなり良かったものの、より柔軟な規制で、比較的一般的です。
(参照公式サイト

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.二次元スキャッタ

コード:

# 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:


3次元散布

コード:
#--------------------------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:



出力:

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.まとめ


  • (1)共通の二次元調整散乱:マーカーのサイズ、種類、色、線の太さ。
  • (2)は、二次元に類似した三次元Axes3Dクラスを使用して、そのような入力のサイズ、寸法、色ベクトルとして、異なるポイントの同じ数である必要があり、缶プログラム体験


免責事項:この記事はブロガーオリジナル記事です、許さブロガーなく再生してはなりません。

公開された14元の記事 ウォン称賛19 ビュー30000 +

おすすめ

転載: blog.csdn.net/qq_24694761/article/details/79201039