今天總結(jié)下Matplotlib的散點(diǎn)圖表蝙。
## pip install matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
利用numpy創(chuàng)建一組數(shù)組,做為y軸。
num=100
np.random.seed(3)
x=np.array(range(1,num+1))
y=1.2*x+4+np.random.randn(num)*5
z=1.3*x+10+np.random.randn(num)*6
直接先畫圖呵哨,再做調(diào)整。
散點(diǎn)圖常用參數(shù)總結(jié)
matplotlib.pyplot.scatter(x, y,
s=20,
c='b',
marker='o',
alpha=None)
x轨奄,y:表示的是shape大小為(n,)的數(shù)組孟害,也就是我們即將繪制散點(diǎn)圖的數(shù)據(jù)點(diǎn),輸入數(shù)據(jù)挪拟。
s:表示的是大小挨务,是一個(gè)標(biāo)量或者是一個(gè)shape大小為(n,)的數(shù)組,可選,默認(rèn)20谎柄。
c:表示的是色彩或顏色序列丁侄,可選,默認(rèn)藍(lán)色’b’朝巫。但是c不應(yīng)該是一個(gè)單一的RGB數(shù)字鸿摇,也不應(yīng)該是一個(gè)RGBA的序列,因?yàn)椴槐銋^(qū)分劈猿。c可以是一個(gè)RGB或RGBA二維行數(shù)組拙吉。
marker:MarkerStyle,表示的是標(biāo)記的樣式揪荣,可選庐镐,默認(rèn)’o’。
alpha:標(biāo)量变逃,0-1之間,可選怠堪,默認(rèn)None揽乱。
plt.figure(figsize=(12,8))
plt.title('this is a scatter')
plt.scatter(x[0:10], y[0:10], s=50, c="m", marker='o')
給C一個(gè)序列,畫個(gè)彩色的氣泡圖,plt.colorbar()加個(gè)顏色標(biāo)注
plt.figure(figsize=(12,8))
plt.title('This is a scatter')
plt.xlabel('x-value')
plt.ylabel('y-label')
sizes =1000*np.random.rand(100)
colors=np.random.rand(100)
plt.scatter(y, z, s=sizes, c=colors, label='Data 1',alpha=0.3,cmap='spring')
plt.colorbar()
plt.grid() # 生成網(wǎng)格
plt.show()
畫氣泡圖粟矿,可以用plt.annotate()函數(shù)用于標(biāo)注文字凰棉。
s 為注釋文本內(nèi)容
xy 為被注釋的坐標(biāo)點(diǎn)
xytext 為注釋文字的坐標(biāo)位置
xycoords and textcoords 是坐標(biāo)xy與xytext的說明
weight 設(shè)置字體線型
color 設(shè)置字體顏色
arrowprops #箭頭參數(shù),參數(shù)類型為字典dict
bbox給標(biāo)題增加外框
plt.figure(figsize=(12,8))
plt.title('This is a scatter')
plt.xlabel('x-value')
plt.ylabel('y-label')
sizes =1000*np.random.rand(100)
colors=np.random.rand(100)
plt.scatter(y, z, s=sizes, c=colors, label='Data 1',alpha=0.3,cmap='spring')
plt.colorbar()
plt.annotate(r'this is a point', xy=(y[10], z[10]), xycoords='data',
xytext=(+10, +30),textcoords='offset points',
fontsize=10,arrowprops=dict(arrowstyle='->',
connectionstyle="arc3,rad=.2"))
plt.grid() # 生成網(wǎng)格
plt.show()