Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
是python畫圖系列第三篇--餅圖
畫餅圖用到的方法為:
matplotlib.pyplot.pie()
參數(shù)為:
pie(x, explode=None, labels=None, colors=('b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'), autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center = (0, 0), frame = False )
參數(shù)說明:
x (每一塊)的比例,如果sum(x) > 1會(huì)使用sum(x)歸一化
labels (每一塊)餅圖外側(cè)顯示的說明文字
explode (每一塊)離開中心距離
startangle 起始繪制角度,默認(rèn)圖是從x軸正方向逆時(shí)針畫起,如設(shè)定=90則從y軸正方向畫起
shadow 是否陰影
labeldistance label繪制位置,相對(duì)于半徑的比例, 如<1則繪制在餅圖內(nèi)側(cè)
autopct 控制餅圖內(nèi)百分比設(shè)置,可以使用format字符串或者format function
'%1.1f'指小數(shù)點(diǎn)前后位數(shù)(沒有用空格補(bǔ)齊)
pctdistance 類似于labeldistance,指定autopct的位置刻度
radius 控制餅圖半徑
返回值:
如果沒有設(shè)置autopct,返回(patches, texts)
如果設(shè)置autopct,返回(patches, texts, autotexts)
patches -- list --matplotlib.patches.Wedge對(duì)象
texts autotexts -- matplotlib.text.Text對(duì)象
下面是一個(gè)簡單的示例:
# -*- coding: utf-8 -*- import numpy as np import matplotlib.mlab as mlab import matplotlib.pyplot as plt labels=['China','Swiss','USA','UK','Laos','Spain'] X=[222,42,455,664,454,334] fig = plt.figure() plt.pie(X,labels=labels,autopct='%1.2f%%') #畫餅圖(數(shù)據(jù),數(shù)據(jù)對(duì)應(yīng)的標(biāo)簽,百分?jǐn)?shù)保留兩位小數(shù)點(diǎn)) plt.title("Pie chart") plt.show() plt.savefig("PieChart.jpg")
下面是結(jié)果:
下面是另一個(gè)示例:
# -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl def draw_pie(labels,quants): # make a square figure plt.figure(1, figsize=(6,6)) # For China, make the piece explode a bit expl = [0,0.1,0,0,0,0,0,0,0,0] #第二塊即China離開圓心0.1 # Colors used. Recycle if not enough. colors = ["blue","red","coral","green","yellow","orange"] #設(shè)置顏色(循環(huán)顯示) # Pie Plot # autopct: format of "percent" string;百分?jǐn)?shù)格式 plt.pie(quants, explode=expl, colors=colors, labels=labels, autopct='%1.1f%%',pctdistance=0.8, shadow=True) plt.title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5}) plt.show() plt.savefig("pie.jpg") plt.close() # quants: GDP # labels: country name labels = ['USA', 'China', 'India', 'Japan', 'Germany', 'Russia', 'Brazil', 'UK', 'France', 'Italy'] quants = [15094025.0, 11299967.0, 4457784.0, 4440376.0, 3099080.0, 2383402.0, 2293954.0, 2260803.0, 2217900.0, 1846950.0] draw_pie(labels,quants)
官方文檔:
鏈接:http://matplotlib.org/api/pyplot_api.html
matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None,radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, hold=None, data=None)Plot a pie chart.
Call signature:
pie(x, explode=None, labels=None, colors=('b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'), autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center = (0, 0), frame = False )
Make a pie chart of array x. The fractional area of each wedge is given by x/sum(x). If sum(x) <= 1, then the values of x give the fractional area directly and the array will not be normalized. The wedges are plotted counterclockwise, by default starting from the x-axis.
Keyword arguments:
explode: [ None | len(x) sequence ]
If not None, is a len(x) array which specifies the fraction of the radius with which to offset each wedge.
colors: [ None | color sequence ]
A sequence of matplotlib color args through which the pie chart will cycle.
labels: [ None | len(x) sequence of strings ]
A sequence of strings providing the labels for each wedge
autopct: [ None | format string | format function ]
If not None, is a string or function used to label the wedges with their numeric value. The label will be placed inside the wedge. If it is a format string, the label will be fmt%pct. If it is a function, it will be called.
pctdistance: scalar
The ratio between the center of each pie slice and the start of the text generated by autopct. Ignored if autopct is None; default is 0.6.
labeldistance: scalar
The radial distance at which the pie labels are drawn
shadow: [ False | True ]
Draw a shadow beneath the pie.
startangle: [ None | Offset angle ]
If not None, rotates the start of the pie chart by angle degrees counterclockwise from the x-axis.
radius: [ None | scalar ] The radius of the pie, if radius is None it will be set to 1.
counterclock: [ False | True ]
Specify fractions direction, clockwise or counterclockwise.
wedgeprops: [ None | dict of key value pairs ]
Dict of arguments passed to the wedge objects making the pie. For example, you can pass in wedgeprops = { ‘linewidth’ : 3 } to set the width of the wedge border lines equal to 3. For more details, look at the doc/arguments of the wedge object. By default clip_on=False.
textprops: [ None | dict of key value pairs ]
Dict of arguments to pass to the text objects.
center: [ (0,0) | sequence of 2 scalars ] Center position of the chart.
frame: [ False | True ]
Plot axes frame with the chart.
The pie chart will probably look best if the figure and axes are square, or the Axes aspect is equal. e.g.:
figure(figsize=(8,8)) ax = axes([0.1, 0.1, 0.8, 0.8])
or:
axes(aspect=1)
Return value:
If autopct is None, return the tuple (patches, texts):
If autopct is not None, return the tuple (patches, texts, autotexts), where patches and texts are as above, and autotexts is a list of Textinstances for the numeric labels.
Notes
In addition to the above described arguments, this function can take a data keyword argument. If such a data argument is given, the following arguments are replaced by data[<arg>]:
Additional kwargs: hold = [True|False] overrides default hold state
源:Python數(shù)據(jù)之道
作者:Peter
整理:陽哥
前不久,陽哥在「Python數(shù)據(jù)之道」分享了讀者投稿的文章,較為綜合的介紹了可視化庫 Highcharts ,這個(gè)一個(gè) JavaScript 下的可視化工具,同時(shí)也有 Python 版本。前文鏈接如下:
不少同學(xué)對(duì)這個(gè)工具感興趣,今天來跟大家介紹下如何用這個(gè)工具來繪制餅圖。大家可以對(duì)照自己常用的 Python 庫,看看哪些工具更適合自己。
本文中介紹的是如何利用 python-highcharts 繪制各種餅圖來滿足不同的需求,主要包含:
首先我們看看整體的效果:
整理的代碼如下:
上面的基礎(chǔ)餅圖在 Highcharts 中默認(rèn)是每個(gè)區(qū)塊的顏色是各不相同的,如果我們想每個(gè)區(qū)塊的顏色是相同的,或者某幾個(gè)區(qū)塊的顏色是相同的,該如何操作呢?
首先看看整體的效果圖:
整體的代碼如下:從導(dǎo)入庫到數(shù)據(jù)的添加設(shè)置、以及參數(shù)項(xiàng)的配置等
其中,重點(diǎn)的參數(shù)設(shè)置看這里:
Highcharts 中就是通過 Highcharts.getOptions().colors 來設(shè)置默認(rèn)的顏色。我們改變下設(shè)置,繪制另一種顏色的餅圖:
如果我們想某幾個(gè)區(qū)塊顯示相同的顏色,可以設(shè)置相同的數(shù)值,首先看看具體的效果圖:
可以看到我們將6個(gè)區(qū)塊的顏色分成了3大類,就是通過上面的方法來實(shí)現(xiàn)的。如果我們?cè)O(shè)置成0-5的數(shù)值,即每個(gè)區(qū)塊的顏色各不相同,那么就是基礎(chǔ)餅圖的樣子:
上面提到的各種餅圖都是沒有圖例的,同時(shí)在區(qū)塊中也沒有直接顯示原始數(shù)據(jù),下面介紹方法來實(shí)現(xiàn)這兩種效果:
圖例和數(shù)據(jù)顯示的代碼設(shè)置:
上面介紹了各種單個(gè)餅圖的制作,下面講解如何利用 python-highcharts 制作雙層餅圖。看看整體的效果:
從上圖中我們可以看到:主要是有5種顏色
數(shù)據(jù)中顯示每個(gè)大類中還有子類,比如:MSIE 父類中還有子類 MSIE6.0、MSIE7.0、MSIE8.0、MSIE9.0。現(xiàn)在我們看看代碼中數(shù)據(jù)的顯示:
可以很清晰地看到:先顯示父級(jí)的數(shù)據(jù),再顯示子級(jí)的數(shù)據(jù)。整體的代碼如下:
上面介紹的都是如何制作各種餅圖,下面介紹一種制作 扇形圖 的方法。首先看看整體的效果:
上面顯示了5個(gè)類別的數(shù)據(jù),同時(shí)顯示了圖例,并且在扇形圖中顯示了數(shù)據(jù)。整體的代碼如下:
重點(diǎn)的設(shè)置部分:
本文結(jié)合各種實(shí)際案例介紹了如何利用 python-highcharts 來繪制各種不同需求的餅圖或者扇形圖。通過上面案例的介紹,我們發(fā)現(xiàn)使用 Highcharts 繪制圖形的主要步驟如下:
最后是個(gè)人的一點(diǎn)感覺:利用 Highcharts 來進(jìn)行繪圖的確代碼量很大,基本上畫一個(gè)簡單的餅圖或者柱狀圖都需要大量的代碼(相對(duì)其他自己使用的可視化庫,比如 pyecharts、plotly_express 等)。
但是它的強(qiáng)大之處,應(yīng)該是在于結(jié)合前端的知識(shí),繪制更多動(dòng)態(tài)效果的圖形,讓圖形的可視化效果更美觀
CSS實(shí)現(xiàn)餅圖效果。
沒什么卵用,但有趣的知識(shí)增加了。hello小伙伴們好,我是柴老師。今天我們來繼續(xù)分享一個(gè)好玩的案例。
它有這樣的需求,要求我們用純CS5的方式實(shí)現(xiàn)餅圖。這個(gè)餅圖一共是到4個(gè)顏色,每個(gè)色值占1/4。
大家想想看,如果不讓你用其他的就用純CS5如何來做?我們直接過來一起來實(shí)現(xiàn)一下。其實(shí)實(shí)現(xiàn)這個(gè)功能它的思路也比較容易。我們來一起看一下。
我們可以通過一個(gè)Div給它創(chuàng)建出來,無非添加一個(gè)圓角為50%。這個(gè)圓大家想想看它一共是,然后可以給這個(gè)盒子創(chuàng)建四個(gè)背景,每一個(gè)占90度是不是就實(shí)現(xiàn)這個(gè)小需求了。
這里提供了四個(gè)色值,分別是紅色、綠色、黃色以及紫色。這樣我們的紅色它站的角度是0到90度對(duì)不對(duì)?
綠色它占的角度是90度,然后到270度。再往下我們的黃色它占的是180度,最后這個(gè)紫色它是270度,到我們的360度是不是這樣個(gè)邏輯?邏輯有了以后具體用哪個(gè)技術(shù)來實(shí)現(xiàn)?其實(shí)我們要用到一個(gè)CS5,它叫做conic gradient,這個(gè)函數(shù)可以幫助我們?nèi)?chuàng)建不同的四個(gè)背景以及它相關(guān)的一個(gè)角度。
接下來我們先過來創(chuàng)建個(gè)圓出來。這里我創(chuàng)建了一個(gè)div,然后上面它的寬高都是,如何讓它變成一個(gè)圓?是不是加上一個(gè)圓角就可以了?我們加個(gè)圓角為50%,這樣它就會(huì)出現(xiàn)一個(gè)圓對(duì)不對(duì)?
我們保存一下,先過來看一下我們的圓,大家看它變成了一個(gè)圓,然后接著用我們的關(guān)鍵去創(chuàng)建它的一個(gè)四個(gè)背景。我們先過來給它來一個(gè)background,然后用一下我們這個(gè)核心函數(shù),把它放過來,這個(gè)函數(shù)是不是需要調(diào)用,然后在里面我們依次把這個(gè)顏色和它對(duì)應(yīng)的角度給它渲染出來就可以了。
第一個(gè)是我們的紅色,紅色它所占的角度是90度,咱們給一個(gè)90度。第二個(gè)是綠色,它是90度到180度,那我們就先寫一個(gè)90,然后再來一個(gè)green到180。
第二個(gè)green也創(chuàng)建好了,再往后是我們的yellow,是180-270,好yellow 180,再來一個(gè)yellow,它是270結(jié)束。最后一個(gè)是我們的紫色,我們的purple,purple它一直到最后就可以了,從270開始,咱們就寫一個(gè)270,前面這個(gè)別忘了加單位,都給它把單位加上。
大家看一下這個(gè)完整代碼。第一個(gè)顏色值第二個(gè)顏色值,咱們初始和咱們的結(jié)尾都不需要寫這個(gè)區(qū)間。這樣寫完以后我們可以過來預(yù)覽一下,是不是已經(jīng)變成了我們想要這個(gè)樣子。
咱們保存一下過來看,大家看這個(gè)擁有4個(gè)色值的餅圖是不是就已經(jīng)實(shí)現(xiàn)了。后續(xù)大家可以利用這個(gè)函數(shù)實(shí)現(xiàn)簡單的餅圖,大家可以在這個(gè)函數(shù)里邊隨意調(diào)整我們的顏色以及它所占的角度。
今天的分享就先到這里,沒有什么卵用,但是有趣的東西又增加了!好玩
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。