P老闆:小Lo啊,你覺得這幾個圖好看嗎
我:好看,好看
P老闆:我也覺得,這個月的月報,就用這個把,你給我幾個,我看看
我:.....
于是乎,我們今天的目标是什麼!!!
畫個環形圖!!!
今天,我們就來先畫個簡單的
成品長這樣
是不是一模一樣!!(很不要臉)
之前我們有學習過,如何用極坐标去掰彎柱狀圖(柱狀圖:為什麼不放過我??)
仔細看看環形圖,其實,環形圖就是掰彎柱狀圖的兄弟——條形圖!!!!
是不是很簡單!!
在matplotlib裡,柱狀圖用bar
而條形就是barh! 多了一個字母而已的
我們先來畫個條形圖試試看
from matplotlib import pyplot as plt
fig = plt.figure(figsize=[13.44,7.5],facecolor=(235/255,235/255,235/255))
ax1=fig.add_subplot(1,1,1,facecolor=(235/255,235/255,235/255))
plt.yticks(range(0,4,1), fontsize=14)
ax1.barh(height=0.5,width=0.1,y=1,color=(243/255,133/255,36/255))
ax1.barh(height=0.5,width=0.2,y=2,color=(243/255,133/255,36/255))
ax1.barh(height=0.5,width=0.3,y=3,left=0.1,color=(243/255,133/255,36/255))
可以發現,在barh中,控制條形位置和大小的參數主要是height,width,y和left
其中height控制着條形圖的寬度(真的不是width,不是width,不是width),也就是縱向的長度
而width控制的是長度,也就是橫向的寬度
y值控制着條形圖中線在y軸的位置,
left控制從左邊起,那個位置開始畫
然後,我們在add_subplot中打開極坐标
他就變成了這樣!!!
似乎崩了!!
沒關系!!
我們繼續。我們繼續,看看改改參數會有什麼神奇的發現。
fig = plt.figure(figsize=[13.44,7.5],facecolor=(235/255,235/255,235/255))
ax1=fig.add_subplot(1,1,1,facecolor=(235/255,235/255,235/255),projection='polar')
plt.yticks(range(0,5,1), fontsize=14)#設定y軸的的刻度
ax1.barh(height=0.5,width=0.1,y=1,color=(243/255,133/255,36/255))
ax1.barh(height=0.5,width=1,y=2,color=(243/255,133/255,36/255))
ax1.barh(height=0.5,width=1,y=3,color=(243/255,133/255,36/255))
ax1.barh(height=0.5,width=1,y=4,color=(243/255,133/255,36/255))
發現:環形是從0讀開始,順時針的畫,而且width控制的是環形的長度
是以環形圖的width也就是實際要畫的圖的X值
然後再height調細
調細後就變成了,和例子差不多的
然後再看看如何打上前端的小點點!
from matplotlib import pyplot as plt
fig = plt.figure(figsize=[13.44,7.5],facecolor=(235/255,235/255,235/255))
ax1=fig.add_subplot(1,1,1,facecolor=(235/255,235/255,235/255),projection='polar')
plt.yticks(range(0,5,1), fontsize=14)#設定y軸的的刻度
ax1.barh(height=0.05,width=1,y=2,color=(243/255,133/255,36/255))
ax1.scatter(x=1,y=2,color=(243/255,133/255,36/255))
ax1.barh(height=0.05,width=1,y=3,color=(243/255,133/255,36/255))
ax1.scatter(x=1,y=3,color=(243/255,133/255,36/255))
ax1.barh(height=0.05,width=1,y=4,color=(243/255,133/255,36/255))
ax1.scatter(x=1,y=4,color=(243/255,133/255,36/255))
用scatter,然後barh的width變成了scatter的x,y還是那個y
我們在來做個小實驗,
我們都知道極坐标應該輸入的是θ和r,一個表示角度,一個表示半徑,才能确定一個點的位置
那怎麼在barh裡換算θ和r呢
比如我們要畫一個這樣的弧形
弧形對應90度角,從45度開始,一直到135度
from matplotlib import pyplot as plt
import numpy as np
fig = plt.figure(figsize=[13.44,7.5],facecolor=(235/255,235/255,235/255))
ax1=fig.add_subplot(1,1,1,facecolor=(235/255,235/255,235/255),projection='polar')
plt.yticks(range(0,5,1), fontsize=14)#設定y軸的的刻度
ax1.barh(height=0.05,width=1,y=2,color=(243/255,133/255,36/255))
ax1.scatter(x=1,y=2,color=(243/255,133/255,36/255))
ax1.barh(height=0.05,width=1,y=3,color=(243/255,133/255,36/255))
ax1.scatter(x=1,y=3,color=(243/255,133/255,36/255))
ax1.barh(height=0.05,width=1*1/4*2*np.pi,y=4,left=1/4*np.pi,color=(243/255,133/255,36/255))
width=1*1/4*2*np.pi,y=4,left=1/4*np.pi
弧形就是圓周的截取,圓周長公式:d*2π,至今乘以2π
最開始,筆者一直以為直徑應該是y值,也就是用2y* 2π 來算直徑
後來我發現,我真的錯,我一開始就不該想的這麼簡單
後來多次嘗試後!
才發現,原來TM的每個圓都是機關圓,直徑都是1!!!!!
是以你要長的弧長,就直接算就好!
比如,你要個半弧形
width就是1*1/2*2*np.pi
最後,去掉網格線就搞定啦!
或者你想要換個方向的弧形,比如例子
你就把width的值加個符号就好了
from matplotlib import pyplot as plt
import numpy as np
fig = plt.figure(figsize=[13.44,7.5],facecolor=(235/255,235/255,235/255))
ax1=fig.add_subplot(1,1,1,facecolor=(235/255,235/255,235/255),projection='polar')
ax1.axis('off')
ax1.barh(height=0.005,width=-0.4*3,y=0.4,color=(243/255,133/255,36/255))
ax1.scatter(-0.4*3,0.4,color=(243/255,133/255,36/255))
ax1.barh(height=0.005,width=-0.5*3,y=0.5,color=(243/255,10/255,36/255))
ax1.scatter(-0.5*3,0.5,color=(243/255,10/255,36/255))
ax1.barh(height=0.005,width=-2*2,y=0.6,color=(243/255,133/255,36/255))
ax1.scatter(-2*2,0.6,color=(243/255,133/255,36/255))
ax1.barh(height=0.005,width=-0.5*np.pi*2,y=0.7,color=(243/255,133/255,36/255))
ax1.scatter(-0.5*np.pi*2,y=0.7,color=(243/255,133/255,36/255))
至于這麼畫出這種效果
就當小作業了,多思考把
完畢,撒花*********