詳解Python+Matplotlib繪制面積圖&熱力圖
1.繪制面積圖
面積圖常用于描述某指標(biāo)隨時間的變化程度。其面積也通常可以有一定的含義。
繪制面積圖使用的是plt.stackplot()方法。
以小學(xué)時期學(xué)的 常見的追擊相遇問題中的速度時間圖像為例,下邊繪制出一幅簡單的v-t圖像。
全局字體設(shè)為默認(rèn)的黑體,時間為從第0秒到第10秒,描述的是甲乙兩個物體的速度。顯然,面積則表示位移。
標(biāo)題部分字體使用楷體(將系統(tǒng)中的TTF字體文件"STKAITI.TTF"復(fù)制到了當(dāng)前目錄下)。
import matplotlib.pyplot as plt
from matplotlib import font_manager
fig = plt.figure(1, facecolor='#ffffcc', figsize=(6, 6))
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['axes.facecolor'] = '#cc00ff'
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
v1 = [1, 1, 1, 2, 3, 4, 5, 5, 5, 5, 5]
v2 = [0.5, 0.5, 0.5, 1, 1.5, 2, 2.5, 3, 3, 2, 1]
plt.stackplot(x, v1, color=['#ff0000'])
plt.stackplot(x, v2, color=['#33ff66'])
plt.xlim(0, 10)
plt.title('v-t圖像', fontsize=25, color='#0033cc', fontproperties=font_manager.FontProperties(fname="STKAITI.TTF"))
plt.xlabel('t/s')
plt.ylabel('v/(m/s)')
plt.legend(['甲', '乙'], bbox_to_anchor=(0.2, 0.95))
plt.show()圖像效果呈現(xiàn)如下:

2.繪制熱力圖
在數(shù)據(jù)分析中,熱力圖也是一種常用的方法,熱力圖通過色差、亮度來展示數(shù)據(jù)與數(shù)據(jù)之間的差異。
繪制熱力圖使用的是plt.imshow()方法,這個方法也即matplotlib中圖像處理常用的方法。
下邊做一個熱力圖的案例示例:
import matplotlib.pyplot as plt
from matplotlib import font_manager
import numpy as np
np.random.seed(30)
data = np.random.randint(70, 100, (30, 8))
plt.imshow(data)
plt.xticks(range(0, 8), ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'])
plt.yticks(range(0, 30), np.array(range(1, 31), dtype='U3'))
# 顯示顏色條
plt.colorbar()
plt.title('30個產(chǎn)品的ABCDEFGH指標(biāo)熱力圖', fontsize=25, color='#0033cc', fontproperties=font_manager.FontProperties(fname="STKAITI.TTF"))
plt.show()
圖像效果呈現(xiàn)如下:

到此這篇關(guān)于詳解Python+Matplotlib繪制面積圖&熱力圖的文章就介紹到這了,更多相關(guān)Python Matplotlib面積圖 熱力圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何使用Python修改matplotlib.pyplot.colorbar的位置以對齊主圖
使用matplotlib.colors模塊可以完成大多數(shù)常見的任務(wù),下面這篇文章主要給大家介紹了關(guān)于如何使用Python修改matplotlib.pyplot.colorbar的位置以對齊主圖的相關(guān)資料,需要的朋友可以參考下2022-07-07
Python數(shù)據(jù)類型之Number數(shù)字操作實例詳解
這篇文章主要介紹了Python數(shù)據(jù)類型之Number數(shù)字操作,結(jié)合實例形式詳細(xì)分析了Python數(shù)字類型的概念、功能、分類及常用數(shù)學(xué)函數(shù)相關(guān)使用技巧,需要的朋友可以參考下2019-05-05

