欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python可視化函數(shù)plt.scatter詳解

 更新時間:2023年04月10日 10:03:54   作者:無水先生  
這篇文章主要介紹了Python可視化函數(shù)plt.scatter詳解,?關(guān)于matplotlib的scatter函數(shù)有許多活動參數(shù),如果不專門注解,是無法掌握精髓的,本文專門針對scatter的參數(shù)和調(diào)用說起,并配有若干案例,需要的朋友可以參考下

一、說明

       關(guān)于matplotlib的scatter函數(shù)有許多活動參數(shù),如果不專門注解,是無法掌握精髓的,本文專門針對scatter的參數(shù)和調(diào)用說起,并配有若干案例。

二、函數(shù)和參數(shù)詳解

2.1 scatter函數(shù)原型

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)

2.2 參數(shù)詳解

屬性參數(shù)意義
坐標x,y輸入點列的數(shù)組,長度都是size
點大小s點的直徑數(shù)組,默認直徑20,長度最大size
點顏色c點的顏色,默認藍色 'b',也可以是個 RGB 或 RGBA 二維行數(shù)組。
點形狀marker點的樣式,默認小圓圈 'o'。
調(diào)色板cmap

Colormap,默認 None,標量或者是一個 colormap 的名字,只有 c 是一個浮點數(shù)數(shù)組時才使用。如果沒有申明就是 image.cmap。

亮度(1)normNormalize,默認 None,數(shù)據(jù)亮度在 0-1 之間,只有 c 是一個浮點數(shù)的數(shù)組的時才使用。
亮度(2)vmin,vmax亮度設置,在 norm 參數(shù)存在時會忽略。
透明度alpha透明度設置,0-1 之間,默認 None,即不透明
linewidths標記點的長度
顏色

edgecolors

顏色或顏色序列,默認為 'face',可選值有 'face', 'none', None。

plotnonfinite

布爾值,設置是否使用非限定的 c ( inf, -inf 或 nan) 繪制點。

**kwargs

其他參數(shù)。

2.3 其中散點的形狀參數(shù)marker如下:

2.4 其中顏色參數(shù)c如下:

三、畫圖示例

3.1 關(guān)于坐標x,y和s,c

import numpy as np
import matplotlib.pyplot as plt
 
# Fixing random state for reproducibility
np.random.seed(19680801)
 
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)          # 顏色可以隨機
area = (30 * np.random.rand(N))**2  # 點的寬度30,半徑15
 
plt.scatter(x, y, s=area, c=colors, alpha=0.5)  
plt.show()

        注意:以上核心語句是:

plt.scatter(x, y, s=area, c=colors, alpha=0.5, marker=",")

        其中:x,y,s,c維度一樣就能成。

3.2 多元高斯的情況

import numpy as np
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(8,6))
#Generating a Gaussion dataset:
#creating random vectors from the multivariate normal distribution
#given mean and covariance
mu_vec1=np.array([0,0])
cov_mat1=np.array([[1,0],[0,1]])
X=np.random.multivariate_normal(mu_vec1,cov_mat1,500)
R=X**2
R_sum=R.sum(axis=1)
plt.scatter(X[:,0],X[:,1],color='green',marker='o', =32.*R_sum,edgecolor='black',alpha=0.5)
 
plt.show()

3.3  繪制例子

from matplotlib import pyplot as plt
import numpy as np
# Generating a Gaussion dTset:
#Creating random vectors from the multivaritate normal distribution
#givem mean and covariance
 
mu_vecl = np.array([0, 0])
cov_matl = np.array([[2,0],[0,2]])
 
x1_samples = np.random.multivariate_normal(mu_vecl, cov_matl,100)
x2_samples = np.random.multivariate_normal(mu_vecl+0.2, cov_matl +0.2, 100)
x3_samples = np.random.multivariate_normal(mu_vecl+0.4, cov_matl +0.4, 100)
 
plt.figure(figsize = (8, 6))
 
plt.scatter(x1_samples[:,0], x1_samples[:, 1], marker='x',
           color = 'blue', alpha=0.7, label = 'x1 samples')
plt.scatter(x2_samples[:,0], x1_samples[:,1], marker='o',
           color ='green', alpha=0.7, label = 'x2 samples')
plt.scatter(x3_samples[:,0], x1_samples[:,1], marker='^',
           color ='red', alpha=0.7, label = 'x3 samples')
plt.title('Basic scatter plot')
plt.ylabel('variable X')
plt.xlabel('Variable Y')
plt.legend(loc = 'upper right')
 
plt.show()
 
 
    import matplotlib.pyplot as plt
    
    fig,ax = plt.subplots()
    
    ax.plot([0],[0], marker="o",  markersize=10)
    ax.plot([0.07,0.93],[0,0],    linewidth=10)
    ax.scatter([1],[0],           s=100)
    
    ax.plot([0],[1], marker="o",  markersize=22)
    ax.plot([0.14,0.86],[1,1],    linewidth=22)
    ax.scatter([1],[1],           s=22**2)
    
    plt.show()
![image.png](http://upload-images.jianshu.io/upload_images/8730384-8d27a5015b37ee97.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
 
    import matplotlib.pyplot as plt
    
    for dpi in [72,100,144]:
    
        fig,ax = plt.subplots(figsize=(1.5,2), dpi=dpi)
        ax.set_title("fig.dpi={}".format(dpi))
    
        ax.set_ylim(-3,3)
        ax.set_xlim(-2,2)
    
        ax.scatter([0],[1], s=10**2, 
                   marker="s", linewidth=0, label="100 points^2")
        ax.scatter([1],[1], s=(10*72./fig.dpi)**2, 
                   marker="s", linewidth=0, label="100 pixels^2")
    
        ax.legend(loc=8,framealpha=1, fontsize=8)
    
        fig.savefig("fig{}.png".format(dpi), bbox_inches="tight")
    
    plt.show() 

3.4 繪圖例3

import matplotlib.pyplot as plt
 
for dpi in [72,100,144]:
 
    fig,ax = plt.subplots(figsize=(1.5,2), dpi=dpi)
    ax.set_title("fig.dpi={}".format(dpi))
 
    ax.set_ylim(-3,3)
    ax.set_xlim(-2,2)
 
    ax.scatter([0],[1], s=10**2, 
               marker="s", linewidth=0, label="100 points^2")
    ax.scatter([1],[1], s=(10*72./fig.dpi)**2, 
               marker="s", linewidth=0, label="100 pixels^2")
 
    ax.legend(loc=8,framealpha=1, fontsize=8)
 
    fig.savefig("fig{}.png".format(dpi), bbox_inches="tight")
 
plt.show() 

3.5  同心繪制

plt.scatter(2, 1, s=4000, c='r')
plt.scatter(2, 1, s=1000 ,c='b')
plt.scatter(2, 1, s=10, c='g')

3.6 有標簽繪制

import matplotlib.pyplot as plt
 
x_coords = [0.13, 0.22, 0.39, 0.59, 0.68, 0.74,0.93]
y_coords = [0.75, 0.34, 0.44, 0.52, 0.80, 0.25,0.55]
 
fig = plt.figure(figsize = (8,5))
 
plt.scatter(x_coords, y_coords, marker = 's', s = 50)
for x, y in zip(x_coords, y_coords):
    plt.annotate('(%s,%s)'%(x,y), xy=(x,y),xytext = (0, -10), textcoords = 'offset points',ha = 'center', va = 'top')
plt.xlim([0,1])
plt.ylim([0,1])
plt.show()

3.7 直線劃分

# 2-category classfication with random 2D-sample data
# from a multivariate normal distribution
 
import numpy as np
from matplotlib import pyplot as plt
 
def decision_boundary(x_1):
    """Calculates the x_2 value for plotting the decision boundary."""
#    return 4 - np.sqrt(-x_1**2 + 4*x_1 + 6 + np.log(16))
    return -x_1 + 1
 
# Generating a gaussion dataset:
# creating random vectors from the multivariate normal distribution
# given mean and covariance
 
mu_vec1 = np.array([0,0])
cov_mat1 = np.array([[2,0],[0,2]])
x1_samples = np.random.multivariate_normal(mu_vec1, cov_mat1,100)
mu_vec1 = mu_vec1.reshape(1,2).T # TO 1-COL VECTOR
 
mu_vec2 = np.array([1,2])
cov_mat2 = np.array([[1,0],[0,1]])
x2_samples = np.random.multivariate_normal(mu_vec2, cov_mat2, 100)
mu_vec2 = mu_vec2.reshape(1,2).T # to 2-col vector
 
# Main scatter plot and plot annotation
 
f, ax = plt.subplots(figsize = (7, 7))
ax.scatter(x1_samples[:, 0], x1_samples[:,1], marker = 'o',color = 'green', s=40)
ax.scatter(x2_samples[:, 0], x2_samples[:,1], marker = '^',color = 'blue', s =40)
plt.legend(['Class1 (w1)', 'Class2 (w2)'], loc = 'upper right')
plt.title('Densities of 2 classes with 25 bivariate random patterns each')
plt.ylabel('x2')
plt.xlabel('x1')
ftext = 'p(x|w1) -N(mu1=(0,0)^t, cov1 = I)\np.(x|w2) -N(mu2 = (1, 1)^t), cov2 =I'
plt.figtext(.15,.8, ftext, fontsize = 11, ha ='left')
 
#Adding decision boundary to plot
 
x_1 = np.arange(-5, 5, 0.1)
bound = decision_boundary(x_1)
plt.plot(x_1, bound, 'r--', lw = 3)
 
x_vec = np.linspace(*ax.get_xlim())
x_1 = np.arange(0, 100, 0.05)
 
plt.show()

3.8 曲線劃分

# 2-category classfication with random 2D-sample data
# from a multivariate normal distribution
 
import numpy as np
from matplotlib import pyplot as plt
 
def decision_boundary(x_1):
    """Calculates the x_2 value for plotting the decision boundary."""
    return 4 - np.sqrt(-x_1**2 + 4*x_1 + 6 + np.log(16))
 
# Generating a gaussion dataset:
# creating random vectors from the multivariate normal distribution
# given mean and covariance
 
mu_vec1 = np.array([0,0])
cov_mat1 = np.array([[2,0],[0,2]])
x1_samples = np.random.multivariate_normal(mu_vec1, cov_mat1,100)
mu_vec1 = mu_vec1.reshape(1,2).T # TO 1-COL VECTOR
 
mu_vec2 = np.array([1,2])
cov_mat2 = np.array([[1,0],[0,1]])
x2_samples = np.random.multivariate_normal(mu_vec2, cov_mat2, 100)
mu_vec2 = mu_vec2.reshape(1,2).T # to 2-col vector
 
# Main scatter plot and plot annotation
 
f, ax = plt.subplots(figsize = (7, 7))
ax.scatter(x1_samples[:, 0], x1_samples[:,1], marker = 'o',color = 'green', s=40)
ax.scatter(x2_samples[:, 0], x2_samples[:,1], marker = '^',color = 'blue', s =40)
plt.legend(['Class1 (w1)', 'Class2 (w2)'], loc = 'upper right')
plt.title('Densities of 2 classes with 25 bivariate random patterns each')
plt.ylabel('x2')
plt.xlabel('x1')
ftext = 'p(x|w1) -N(mu1=(0,0)^t, cov1 = I)\np.(x|w2) -N(mu2 = (1, 1)^t), cov2 =I'
plt.figtext(.15,.8, ftext, fontsize = 11, ha ='left')
 
#Adding decision boundary to plot
 
x_1 = np.arange(-5, 5, 0.1)
bound = decision_boundary(x_1)
plt.plot(x_1, bound, 'r--', lw = 3)
 
x_vec = np.linspace(*ax.get_xlim())
x_1 = np.arange(0, 100, 0.05)
 
plt.show()

到此這篇關(guān)于Python可視化函數(shù)plt.scatter詳解的文章就介紹到這了,更多相關(guān)Python plt.scatter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python中幾個常用函數(shù)的正確用法-lambda/filter/map/reduce

    python中幾個常用函數(shù)的正確用法-lambda/filter/map/reduce

    這篇文章主要介紹了python中幾個常用函數(shù)的正確用法,這幾個常用函數(shù)包括lambda、filter、map、reduce,本文將圍繞這幾個常用函數(shù)展開內(nèi)容,需要的朋友可以參考一下
    2021-11-11
  • Pandas之read_csv()讀取文件跳過報錯行的解決

    Pandas之read_csv()讀取文件跳過報錯行的解決

    這篇文章主要介紹了Pandas之read_csv()讀取文件跳過報錯行的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python中virtualenvwrapper安裝與使用

    python中virtualenvwrapper安裝與使用

    本篇文章給大家介紹了python環(huán)境神器virtualenvwrapper安裝與使用,對此有需要的朋友可以跟著操作一下。
    2018-05-05
  • Python獲取系統(tǒng)默認字符編碼的方法

    Python獲取系統(tǒng)默認字符編碼的方法

    這篇文章主要介紹了Python獲取系統(tǒng)默認字符編碼的方法,涉及Python中sys模塊getdefaultencoding方法的使用技巧,需要的朋友可以參考下
    2015-06-06
  • Python一行代碼識別車牌號碼實現(xiàn)示例詳解

    Python一行代碼識別車牌號碼實現(xiàn)示例詳解

    這篇文章主要為大家介紹了Python一行代碼識別車牌號碼實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • python中super().__init__()作用詳解

    python中super().__init__()作用詳解

    super()用來調(diào)用父類(基類)的方法,__init__()是類的構(gòu)造方法,super().__init__()?就是調(diào)用父類的init方法,?同樣可以使用super()去調(diào)用父類的其他方法,這篇文章主要介紹了python中super().__init__(),需要的朋友可以參考下
    2023-02-02
  • 淺析Flask如何使用日志功能

    淺析Flask如何使用日志功能

    這篇文章主要為大家詳細介紹了Flask是如何使用日志功能的,文中的示例代碼講解詳細,對我們深入了解Flask有一定的幫助,需要的可以參考一下
    2023-05-05
  • 如何將python項目部署在一臺服務器上

    如何將python項目部署在一臺服務器上

    服務器less技術(shù)是一種無需管理服務器即可運行應用程序的方法,最流行的服務器less平臺是AWS Lambda,這篇文章主要介紹了如何將python項目部署在一臺服務器上,需要的朋友可以參考下
    2023-10-10
  • PyQt界面阻塞卡死問題的解決

    PyQt界面阻塞卡死問題的解決

    當用PyQt5開發(fā)一個GUI界面 ,需要執(zhí)行業(yè)務邏輯時,后臺邏輯執(zhí)行時間長,界面就容易出現(xiàn)卡死、未響應等問題,本文主要介紹了PyQt界面阻塞卡死問題的解決
    2024-01-01
  • 用pickle存儲Python的原生對象方法

    用pickle存儲Python的原生對象方法

    下面小編就為大家?guī)硪黄胮ickle存儲Python的原生對象方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04

最新評論