matplotlib -> 散點圖
matplotlib -> 散點圖
眾所周知。。Matplotlib可以畫我們工作中基本的一些圖列(沒有營養的開場白....)
散點圖是指在回歸分析中,數據點在直角坐標系平面上的分布圖,散點圖表示因變量隨自變量而變化的大致趨勢,據此可以選擇合適的函數對數據點進行擬合。
用兩組數據構成多個坐標點,考察坐標點的分布,判斷兩變量之間是否存在某種關聯或總結坐標點的分布模式。散點圖將序列顯示為一組點。值由點在圖表中的位置表示。類別由圖表中的不同標記表示。散點圖通常用于比較跨類別的聚合數據。
--來源百度百科

一個簡單的散點圖就完成了
x: x軸數據: 數據集或列表
y: y軸數據: 數據集或列表
s: 點的大小
c: 點的顏色
alpha: 點的透明圖 0-1
加點難度
ax = np.random.randint(1, 40, (1, 20))
ay = np.random.randint(40, 100, (1,20))
az = np.random.randint(100,200,(1,20))
plt.scatter(x=ax, y=ay,c=az, s=100, cmap="jet", alpha=0.4)
plt.show()

事情開始慢慢變得有意思了 上面不是說c是顏色嘛 為什么現在也可以是數據了吶[疑問]?
c: 可以是數據或者顏色 只不過如果是數據的話 顏色就要用另外的參數cmap
關于cmap: 網上有很多理解 解釋 https://www.baidu.com/ 官方給出了一些推薦的色帶?
cmap:





大致就是cmap="上面的色帶" 官網有些詳細的用發 我這里之說干貨 詳情移步--> https://matplotlib.org/stable/gallery/color/colormap_reference.html
然后 一些基本操作[汗]
ax = np.random.randint(1, 40, (1, 20))
ay = np.random.randint(40, 100, (1,20))
az = np.random.randint(100,200,(1,20))
plt.scatter(x=ax, y=ay, c=az, s=100, marker = "^", cmap="jet", alpha=0.4, label="test")
plt.legend()
plt.xlabel("XXXX")
plt.ylabel("YYYY")
plt.gca().spines['top'].set_color('none')
plt.gca().spines['right'].set_color('none')
plt.title("test_scatter")
plt.ylim(40,80)
plt.xlim(1,40)
plt.show()

有些需求會涉及到間隔 例如x軸間隔 y軸間隔
ax = np.random.randint(1, 40, (1, 20))
ay = np.random.randint(40, 100, (1,20))
az = np.random.randint(100,200,(1,20))
plt.scatter(x=ax, y=ay, c=az, s=100, marker = "^", cmap="jet", alpha=0.4, label="test")
plt.legend()
plt.xlabel("XXXX")
plt.ylabel("YYYY")
plt.gca().spines['top'].set_color('none')
plt.gca().spines['right'].set_color('none')
plt.title("test_scatter")
plt.ylim(40,80)
plt.xlim(1,40)
major_locator = plt.MultipleLocator(20)
plt.gca().xaxis.set_major_locator(major_locator)
plt.gca().yaxis.set_major_locator(major_locator)
plt.show()

一些奇怪的需求 拼色帶
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors as cols
ax = np.random.randint(1, 40, (1, 20))
ay = np.random.randint(40, 100, (1,20))
az = np.random.randint(100,200,(1,20))
colordict = ["#B8F8FA", "#00FA9A", "#32CD32", "#00BFFF", "#0000FF", "#008000", "#EE82EE", "#6F2322", ]
map_colors = cols.ListedColormap(colordict, )
plt.scatter(x=ax, y=ay, c=az, s=100, marker = "^", cmap=map_colors, alpha=0.4, label="test")
plt.legend()
plt.xlabel("XXXX")
plt.ylabel("YYYY")
plt.gca().spines['top'].set_color('none')
plt.gca().spines['right'].set_color('none')
plt.title("test_scatter")
plt.ylim(40,80)
plt.xlim(1,40)
major_locator = plt.MultipleLocator(20)
plt.gca().xaxis.set_major_locator(major_locator)
plt.gca().yaxis.set_major_locator(major_locator)
plt.show()

matplotlib.colors 對顏色進行操作 拼接色帶(非專業matplotlib 主要功能實現就OK)
16進制顏色 或其他可識別顏色皆可拼接 本質還是 數值大小==顏色深淺
還有一個重頭操作曾經折磨我好長時間 colorbar
colorbar
將色帶圖例標注在圖上
這個操作我也是摸索很久 網上不少大佬和萎大佬寫的五迷三道 就怕我看懂似的 唉 所以還是自己做下總結吧
首先看下樣子

嘟~ 很簡單的一個小玩意
# todo 具體實現 colorbar plt.colorbar()
ax = np.random.randint(1, 40, (1, 20))
ay = np.random.randint(40, 100, (1,20))
az = np.random.randint(100,200,(1,20))
colordict = ["#B8F8FA", "#00FA9A", "#32CD32", "#00BFFF", "#0000FF", "#008000", "#EE82EE", "#6F2322", ]
map_colors = cols.ListedColormap(colordict, )
plt.scatter(x=ax, y=ay, c=az, s=100, marker = "^", cmap=map_colors, alpha=0.4, label="test")
plt.colorbar()
plt.legend()
plt.xlabel("XXXX")
plt.ylabel("YYYY")
plt.gca().spines['top'].set_color('none')
plt.gca().spines['right'].set_color('none')
plt.title("test_scatter")
plt.ylim(40,80)
plt.xlim(1,40)
major_locator = plt.MultipleLocator(20)
plt.gca().xaxis.set_major_locator(major_locator)
plt.gca().yaxis.set_major_locator(major_locator)
plt.show()
默認貼著上一個畫圖邏輯 會豎著立到散點圖的右邊 可能有些需求會要求 圖例放到下邊(這個東西也查了好久 畢竟剛接觸 會懵逼[苦澀]) 不墨跡 參數如下 ?
plt.colorbar(orientation, shrink, pad, cax, ax)
orientation: 默認豎放 可改參數 ”horizontal”(默認橫放 放在圖的下方)
shrink: colorbar縮小比例 0-1
pad: 距離圖的位置 (0.1親測好用)
cax,ax ..... 據聽說是可以調節colorbar的位置 據聽說哈 不知道咋用[擦汗]
如果就這么簡單就好了.. 上面完成后看一下效果
ax = np.random.randint(1, 40, (1, 20))
ay = np.random.randint(40, 100, (1,20))
az = np.random.randint(100,200,(1,20))
colordict = ["#B8F8FA", "#00FA9A", "#32CD32", "#00BFFF", "#0000FF", "#008000", "#EE82EE", "#6F2322", ]
map_colors = cols.ListedColormap(colordict, )
plt.scatter(x=ax, y=ay, c=az, s=100, marker = "^", cmap=map_colors, alpha=0.4, label="test")
plt.colorbar(orientation="horizontal",shrink=0.5,pad=0.1)
plt.legend()
plt.xlabel("XXXX")
plt.ylabel("YYYY")
plt.gca().spines['top'].set_color('none')
plt.gca().spines['right'].set_color('none')
plt.title("test_scatter")
plt.ylim(40,80)
plt.xlim(1,40)
major_locator = plt.MultipleLocator(20)
plt.gca().xaxis.set_major_locator(major_locator)
plt.gca().yaxis.set_major_locator(major_locator)
plt.show()

當然還有一些其他參數 如ticks 可以不讓圖例的標度按照數值c走 dd 其他參數 也可以設置 title label 等
如果需要對刻度寫一個單位的話 建議使用 title 下面的寫法萌新就這么寫就OK
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
ax = np.random.randint(1, 40, (1, 20))
ay = np.random.randint(40, 100, (1,20))
az = np.random.randint(100,200,(1,20))
colordict = ["#B8F8FA", "#00FA9A", "#32CD32", "#00BFFF", "#0000FF", "#008000", "#EE82EE", "#6F2322", ]
map_colors = cols.ListedColormap(colordict, )
plt.scatter(x=ax, y=ay, c=az, s=100, marker = "^", cmap=map_colors, alpha=0.4, label="test")
cb = plt.colorbar(orientation="horizontal",shrink=0.5,pad=0.15)
cb.ax.set_title(r'單位:絕絕子', fontsize=11, x=1.25, y=-1.2)
plt.legend()
plt.xlabel("XXXX")
plt.ylabel("YYYY")
plt.gca().spines['top'].set_color('none')
plt.gca().spines['right'].set_color('none')
plt.title("test_scatter")
plt.ylim(40,80)
plt.xlim(1,40)
major_locator = plt.MultipleLocator(20)
plt.gca().xaxis.set_major_locator(major_locator)
plt.gca().yaxis.set_major_locator(major_locator)
plt.show()

# 解決中文亂碼問題
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
有的小萌新剛抬著鋼筋下了工地要問 為啥要用 set_title 不用set_label?
源碼如是
def set_label(self, label, *, loc=None, **kwargs):
"""
Add a label to the long axis of the colorbar.
Parameters
----------
label : str
The label text.
loc : str, optional
The location of the label.
- For horizontal orientation one of {'left', 'center', 'right'}
- For vertical orientation one of {'bottom', 'center', 'top'}
Defaults to :rc:`xaxis.labellocation` or :rc:`yaxis.labellocation`
depending on the orientation.
**kwargs
Keyword arguments are passed to `~.Axes.set_xlabel` /
`~.Axes.set_ylabel`.
Supported keywords are *labelpad* and `.Text` properties.
"""
if self.orientation == "vertical":
self.ax.set_ylabel(label, loc=loc, **kwargs)
else:
self.ax.set_xlabel(label, loc=loc, **kwargs)
self.stale = True
if self.orientation == "vertical": self.ax.set_ylabel(label, loc=loc, **kwargs)
嗷?(根本沒有小萌新 就是我[苦澀])
如果用set_label的 只可以在x軸方向移動 y軸?想到不要想
cb.ax.set_title(r'單位:絕絕子', fontsize=11, x=1.25, y=-1.2)
fontsize: title的字體大小
x: x軸方向 +-float or +=-int
y: y軸方向 +-float or +=-int
剩下就自己摸索調整吧
忘了點事 可以會有的需求吧
我要我的數值對標顏色范圍! 必須馬上現在
再次拼接色帶
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
ax = np.random.randint(1, 100, (1, 80))
ay = np.random.randint(1, 100, (1, 80))
az = np.random.randint(100,200,(1,80))
colorlevel = [100, 120, 121, 130, 131, 140, 141, 160, 181.0, 200.0 ]
colordict = ["#B8F8FA", "#00FA9A", "#32CD32", "#00BFFF", "#0000FF", "#008000", "#EE82EE", "#6F2322", ]
map_colors = cols.ListedColormap(colordict, )
norm = cols.BoundaryNorm(colorlevel, map_colors.N, )
plt.scatter(x=ax, y=ay, c=az, s=100, marker = "^", cmap=map_colors, alpha=0.4, label="test")
cb = plt.colorbar(orientation="horizontal",shrink=0.5,pad=0.15)
cb.ax.set_title(r'單位:絕絕子', fontsize=11, x=1.25, y=-1.2)
plt.legend()
plt.xlabel("XXXX")
plt.ylabel("YYYY")
plt.gca().spines['top'].set_color('none')
plt.gca().spines['right'].set_color('none')
plt.title("test_scatter")
plt.ylim(40,80)
plt.xlim(1,40)
major_locator = plt.MultipleLocator(20)
plt.gca().xaxis.set_major_locator(major_locator)
plt.gca().yaxis.set_major_locator(major_locator)
plt.show()

上面寫的ticks 可以加入進來
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
ax = np.random.randint(1, 100, (1, 80))
ay = np.random.randint(1, 100, (1, 80))
az = np.random.randint(100,200,(1,80))
colorlevel = [100, 120, 121, 130, 131, 140, 141, 160, 181.0, 200.0 ]
colordict = ["#B8F8FA", "#00FA9A", "#32CD32", "#00BFFF", "#0000FF", "#008000", "#EE82EE", "#6F2322", ]
map_colors = cols.ListedColormap(colordict, )
norm = cols.BoundaryNorm(colorlevel, map_colors.N, )
plt.scatter(x=ax, y=ay, c=az, s=100, marker = "^", cmap=map_colors, alpha=0.4, label="test")
cb = plt.colorbar(orientation="horizontal",shrink=0.5,pad=0.15,ticks=[i for i in range(1,300,10)])
cb.ax.set_title(r'單位:絕絕子', fontsize=11, x=1.25, y=-1.2)
plt.legend()
plt.xlabel("XXXX")
plt.ylabel("YYYY")
plt.gca().spines['top'].set_color('none')
plt.gca().spines['right'].set_color('none')
plt.title("test_scatter")
plt.ylim(40,80)
plt.xlim(1,40)
major_locator = plt.MultipleLocator(20)
plt.gca().xaxis.set_major_locator(major_locator)
plt.gca().yaxis.set_major_locator(major_locator)
plt.show()

丑了點 理解就OK。。
僅供參考

浙公網安備 33010602011771號