用python画面积图_Python笔记:用matplotlib绘制面积图
用pyecharts繪制出來的面積圖、圓環(huán)圖太有個(gè)性了,未免感覺有點(diǎn)“高處不勝寒”。今晚雖然休息,但繼用matplotlib繪制折線圖之后,我又心血來潮地想用matplotlib來制作一點(diǎn)面積圖,因?yàn)檫@在零售行業(yè)也是常用的可視化圖形之一。具體如下:
繪制常規(guī)面積圖(餅圖):
# 導(dǎo)入庫
import matplotlib.pyplot as plt
import numpy as np
# 在線顯示
%matplotlib inline
# 讓中文標(biāo)簽正常顯示
plt.rcParams["font.sans-serif"] = ["SimHei"]
# 讓負(fù)號(hào)正常顯示
plt.rcParams["axes.unicode_minus"] = False
# 調(diào)整畫布的大小
fig = plt.figure(figsize = (10,10))
# 準(zhǔn)備待傳入的參數(shù)
# 數(shù)值
data=[100,125,135,145,150]
# 各扇區(qū)的我顏色
colors = ["DarkMagenta", "Teal", "pink", "SlateGray", "Tomato"]
# 對(duì)應(yīng)的標(biāo)簽
labels = ["蔬菜", "水果", "水產(chǎn)", "豬肉", "綜合"]
# 直接繪制出一個(gè)簡(jiǎn)單而又原始的餅圖
# plt.pie(data, labels = labels)
# 調(diào)整各扇區(qū)的顏色
# plt.pie(data, labels = labels, colors = colors)
# autopct 讓百分比正常顯示
# plt.pie(data, colors = colors, labels = labels,autopct = " %1.1f%% ")
# labeldistance 調(diào)節(jié)各扇區(qū)標(biāo)簽距離圓心的距離
# plt.pie(data, colors = colors, labels = labels,autopct = " %1.1f%% ",labeldistance = 0.4)
# radius 調(diào)整扇形的大小
# plt.pie(data, colors = colors, labels = labels,autopct = " %1.1f%% ",labeldistance = 0.4, radius = 1)
# counterclock 調(diào)整指針方向,True是逆時(shí)針,False是順時(shí)針
# plt.pie(data, colors = colors, labels = labels,autopct = " %1.1f%% ",labeldistance = 0.4, radius = 1,counterclock = False)
# center 調(diào)整圖表中心位置
plt.pie(data, colors = colors, labels = labels,autopct = " %1.1f%% ",labeldistance = 0.4, radius = 1,counterclock = False,center = (0.5, 0.5))
# 保存輸出餅圖
plt.savefig(r"C:\Users\QDM\Desktop\餅圖.png")
可視化結(jié)果:
繪制圓環(huán)圖:
# 導(dǎo)入庫
import matplotlib.pyplot as plt
import numpy as np
# 在線顯示
%matplotlib inline
# 讓中文標(biāo)簽正常顯示
plt.rcParams["font.sans-serif"] = ["SimHei"]
# 讓負(fù)號(hào)正常顯示
plt.rcParams["axes.unicode_minus"] = False
# 調(diào)整畫布的大小
fig = plt.figure(figsize = (10,6))
# 模擬數(shù)據(jù)
x=[16,41,60]
# 繪制一個(gè)我們自己所需要呈現(xiàn)的面積圖
# plt.pie(x, labels=["上午銷售占比", "下午銷售占比", "晚上銷售占比"])
# 自定義各段區(qū)間的顏色
# plt.pie(x, labels=["上午銷售占比", "下午銷售占比", "晚上銷售占比"],colors = ["m","Crimson","blue"])
# 顯示百分比
# plt.pie(x, labels=["上午銷售占比", "下午銷售占比", "晚上銷售占比"],colors = ["m","Crimson","blue"],autopct = " %1.1f%% ")
# 調(diào)整百分比所顯示的位置
plt.pie(x, labels=["上午銷售占比", "下午銷售占比", "晚上銷售占比"],colors = ["m","Crimson","SteelBlue"],autopct = " %1.1f%% ",pctdistance = 0.8)
# plt.pie(x, labels=["上午銷售占比", "下午銷售占比", "晚上銷售占比"],colors = ["m","#f00056","Orchid"],autopct = " %1.1f%% ",pctdistance = 0.8,labeldistance = 1)
# 在第一個(gè)餅圖的基礎(chǔ)上再畫一個(gè)顏色為白色,半徑較小的餅圖進(jìn)行覆蓋,就可以畫出圓環(huán)圖
plt.pie([1],colors = "w",radius = 0.6)
plt.axis("equal")
plt.legend()
# 保存輸出環(huán)形圖
plt.savefig(r"C:\Users\QDM\Desktop\環(huán)形圖.png")
可視化結(jié)果:
繪制另類的混合圖形:
# 導(dǎo)入庫
import matplotlib.pyplot as plt
import numpy as np
# 在線顯示
%matplotlib inline
# 讓中文標(biāo)簽正常顯示
plt.rcParams["font.sans-serif"] = ["SimHei"]
# 讓負(fù)號(hào)正常顯示
plt.rcParams["axes.unicode_minus"] = False
# 調(diào)整畫布的大小
fig = plt.figure(figsize = (10,6))
# 構(gòu)造數(shù)據(jù)
a = [0.4, 0.15, 0.2, 0.1, 0.15]
b = [0.3, 0.25, 0.16, 0.14, 0.15]
label=["蘋果", "雪梨", "西瓜", "菠蘿", "香瓜"]
# 繪制外圓
plt.pie(a,autopct = " %1.1f%% ", pctdistance = 0.85,labels = label)
# 繪制內(nèi)圓
plt.pie(b,radius = 0.7, autopct = " %1.1f%% ",pctdistance = 0.78,)
plt.axis("equal")
# 保存輸出混合面積圖
plt.savefig(r"C:\Users\QDM\Desktop\混合面積圖.png")
可視化結(jié)果:
小結(jié):雖然不完美,但是歸納知識(shí)的過程很是愉悅!
總結(jié)
以上是生活随笔為你收集整理的用python画面积图_Python笔记:用matplotlib绘制面积图的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle用户获取datameta权限
- 下一篇: 曝小米12 Ultra渲染图不完全准确: