亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python繪制餅圖的方法詳解

 更新時(shí)間:2022年03月17日 17:10:21   作者:Vergil_Zsh  
這篇文章主要為大家詳細(xì)介紹了python繪制餅圖的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助

用法

matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=0, radius=1, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, *, normalize=True, data=None)

參數(shù)介紹

參數(shù) 
x楔形尺寸
explode類似數(shù)組,默認(rèn)值: 無(wú),如果不是無(wú),則是一個(gè)len(x)數(shù)組,用于指定偏移每個(gè)楔塊的半徑
labels標(biāo)簽列表:默認(rèn)值:無(wú),為每個(gè)楔塊提供標(biāo)簽的一系列字符串
colors顏色,默認(rèn)值:無(wú),餅圖循環(huán)使用的一系列顏色,如果沒(méi)有,將使用當(dāng)前活動(dòng)周期中的顏色
autopct默認(rèn)值:無(wú),如果不是無(wú),則是一個(gè)字符串或函數(shù),用于用數(shù)字值標(biāo)記楔塊.標(biāo)簽將放在楔子內(nèi),如果是格式字符串,則標(biāo)簽為fmt%pct,如果是函數(shù),則調(diào)用
pctdistance默認(rèn)值為0.6,每個(gè)餅圖切片的中心與生成的文本開頭之間的比率
shadow默認(rèn)值為:False,楔塊的陰影
labeldistance默認(rèn)值1.1,繪制餅圖標(biāo)簽徑向距離,如果設(shè)置為’無(wú)’,則不會(huì)繪制標(biāo)簽,會(huì)存儲(chǔ)標(biāo)簽以供在圖列()中使用
startangle餅圖角度起始角度
radius默認(rèn)值1,餅圖的半徑,數(shù)值越大,餅圖越大
counterclock設(shè)置餅圖的方向,默認(rèn)值為True,表示逆時(shí)針?lè)较?False,為順時(shí)針
wedgeprops默認(rèn)值:無(wú),傳遞給楔形對(duì)象的參數(shù),設(shè)置楔形的屬性
textprops設(shè)置文本對(duì)象的字典參數(shù)
center浮點(diǎn)類型的列表,可選參數(shù),圖標(biāo)中心位置
frame是否選擇軸框架,默認(rèn)值為False,如果是True,則繪制帶有表的軸框架
rotatelabels默認(rèn)值為False,布爾類型,如果為True,則將每個(gè)標(biāo)簽旋轉(zhuǎn)到相應(yīng)切片的角度
narmalize布爾類型,默認(rèn)值為True,如果為True,則始終通過(guò)規(guī)范化x來(lái)制作完整的餅圖,使總和(x)=1。如果sum(x)<=1,F(xiàn)alse將生成部分餅圖,并為sum(x)>1引發(fā)ValueError。
data可選參數(shù),如果給定,一下參數(shù)接受字符串s,該字符串被解釋為數(shù)據(jù)[s]

案例

x

import numpy as np
import maplotlib.pyplot as plt
x = [1, 2, 3, 4]
plt.pie(x)
plt.show()

在這里插入圖片描述

explode

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus']=False
x = [1, 2, 3, 4]
plt.subplot(121)
plt.title('正常')
plt.pie(x)
plt.subplot(122)
plt.title('添加explode')
plt.pie(x,explode=[0.1,0.2,0.1,0.2])
plt.show()

在這里插入圖片描述

labels,labeldistance

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus']=False
x = [15, 30, 45, 10]
plt.subplot(131)
plt.title('正常')
plt.pie(x)
plt.subplot(132)
plt.title('添加labels')
plt.pie(x,labels=['x1','y1','x2','y2'])
# labeldistance默認(rèn)為是1.1
plt.subplot(133)
plt.title('添加labels和labeldistance')
plt.pie(x,labels=['x1','y1','x2','y2'],labeldistance=1.2)
plt.show()

在這里插入圖片描述

colors

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus']=False
x = [1, 2, 3, 4]
plt.subplot(121)
plt.title('正常')
plt.pie(x)
# 顏色參數(shù)必須保持和x長(zhǎng)度一樣
plt.subplot(122)
colors = plt.get_cmap('Blues')(np.linspace(0.2,0.7,len(x)))
print(colors)
plt.title('添加colors')
plt.pie(x,colors=colors)
plt.show()

在這里插入圖片描述

autopct

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus']=False
x = [1, 2, 3, 4]
plt.subplot(131)
plt.title('正常')
plt.pie(x)
plt.subplot(132)
plt.title('添加autopct為1.1f')
plt.pie(x,autopct='%1.1f%%')
plt.subplot(133)
plt.title('添加autopct為10.1f')
plt.pie(x,autopct='%10.1f%%')
plt.show()

在這里插入圖片描述

pctdistance

import numpy as np
import matplotlib.pyplot as plt

plt.figsize=((10,8))
plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus']=False
x = [1, 2, 3, 4]
plt.subplot(131)
plt.title('正常')
plt.pie(x)
plt.subplot(132)
plt.title('添加pctdistance默認(rèn)值0.6')
plt.pie(x,autopct='%1.1f%%',pctdistance=0.6)
plt.subplot(133)
plt.title('添加pctdistance值1.5')
plt.pie(x,autopct='%1.1f%%',pctdistance=0.8)
plt.show()

在這里插入圖片描述

pctdistance和autopct設(shè)置都可以偏移百分比,一個(gè)是同方向偏移,一個(gè)是距中心點(diǎn)位置

shadow

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus']=False
x = [15, 30, 45, 10]
plt.subplot(121)
plt.title('正常')
plt.pie(x)
plt.subplot(122)
plt.title('添加shadow')
plt.pie(x,explode=(0,0,0.1,0),shadow=True)

plt.show()

在這里插入圖片描述

startangle

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus']=False
x = [15, 30, 45, 10]
plt.subplot(121)
plt.title('正常')
plt.pie(x,autopct='%1.1f%%')
# 起始角度設(shè)置
plt.subplot(122)
plt.title('設(shè)置startangle=90')
plt.pie(x,autopct='%1.1f%%',startangle=90)

plt.show()

在這里插入圖片描述

radius

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus']=False
x = [15, 30, 45, 10]
plt.subplot(121)
plt.title('正常')
plt.pie(x,autopct='%1.1f%%')
plt.subplot(122)
plt.title('設(shè)置radius=0.9')
plt.pie(x,autopct='%1.1f%%',radius=0.9)

plt.show()

在這里插入圖片描述

counterclock

counterclock=False,設(shè)置餅圖方向?yàn)槟娣较?/p>

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus']=False
x = [15, 30, 45, 10]
plt.subplot(121)
plt.title('正常')
plt.pie(x,autopct='%1.1f%%')
plt.subplot(122)
plt.title('設(shè)置counterclock=False')
plt.pie(x,autopct='%1.1f%%',counterclock=False)
plt.show()

在這里插入圖片描述

wedgeprops

設(shè)置楔形的屬性

wedgeprops傳入字典類型,width設(shè)置,可以轉(zhuǎn)變?yōu)榄h(huán)形圖,edgecolor設(shè)置其環(huán)形邊緣顏色

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus']=False
x = [15, 30, 45, 10]
plt.subplot(121)
plt.title('正常')
plt.pie(x,autopct='%1.1f%%')
plt.subplot(122)
plt.title('設(shè)置wedgeprops楔形的屬性')
plt.pie(x,autopct='%1.1f%%',wedgeprops=dict(width=0.3, edgecolor='blue'))

plt.show()

在這里插入圖片描述

textprops,center,frame

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus']=False
x = [15, 30, 45, 10]
plt.subplot(131)
plt.title('正常')
plt.pie(x,autopct='%1.1f%%')
plt.subplot(132)
plt.title('設(shè)置textprops,center=1.1,frame')
plt.pie(x,autopct='%1.1f%%',textprops={'size': 'larger'},center=(1,1),frame=True)

plt.subplot(133)
plt.title('設(shè)置textprops,center=2.2,frame')
plt.pie(x,autopct='%1.1f%%',textprops={'size': 'x-large'},center=(2,2),frame=True)

plt.show()

在這里插入圖片描述

rotatelabels,normalize

這里不多介紹,可根據(jù)上述自己檢驗(yàn),很少被用到

舉例

1.取餅圖一部分楔形,添加colorbar

import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch
import numpy as np
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 5))
fig.subplots_adjust(wspace=0)
ratios = [.27, .56, .17]
labels = ['Approve', 'Disapprove', 'Undecided']
explode = [0.1, 0, 0]
angle = -180 * ratios[0]
ax1.pie(ratios, autopct='%1.1f%%', startangle=angle,
        labels=labels, explode=explode)
xpos = 0
bottom = 0
ratios = [.33, .54, .07, .06]
width = .2
colors = [[.1, .3, .5], [.1, .3, .3], [.1, .3, .7], [.1, .3, .9]]
for j in range(len(ratios)):
    height = ratios[j]
    ax2.bar(xpos, height, width, bottom=bottom, color=colors[j])
    ypos = bottom + ax2.patches[j].get_height() / 2
    bottom += height
    ax2.text(xpos, ypos, "%d%%" % (ax2.patches[j].get_height() * 100),
             ha='center')
ax2.set_title('Age of approvers')
ax2.legend(('50-65', 'Over 65', '35-49', 'Under 35'))
ax2.axis('off')
ax2.set_xlim(- 2.5 * width, 2.5 * width)

theta1, theta2 = ax1.patches[0].theta1, ax1.patches[0].theta2
center, r = ax1.patches[0].center, ax1.patches[0].r
bar_height = sum([item.get_height() for item in ax2.patches])
# draw top connecting line
x = r * np.cos(np.pi / 180 * theta2) + center[0]
y = r * np.sin(np.pi / 180 * theta2) + center[1]
con = ConnectionPatch(xyA=(-width / 2, bar_height), coordsA=ax2.transData,
                      xyB=(x, y), coordsB=ax1.transData)
con.set_color([0, 0, 0])
con.set_linewidth(4)
ax2.add_artist(con)
# draw bottom connecting line
x = r * np.cos(np.pi / 180 * theta1) + center[0]
y = r * np.sin(np.pi / 180 * theta1) + center[1]
con = ConnectionPatch(xyA=(-width / 2, 0), coordsA=ax2.transData,
                      xyB=(x, y), coordsB=ax1.transData)
con.set_color([0, 0, 0])
ax2.add_artist(con)
con.set_linewidth(4)
plt.show()

在這里插入圖片描述

2.環(huán)形圖

import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
size = 0.3
vals = np.array([[60., 32.], [37., 40.], [29., 10.]])
cmap = plt.cm.Set1
outer_colors = cmap(np.arange(3)*4)
inner_colors = cmap([1, 2, 5, 6, 9, 10])
ax.pie(vals.sum(axis=1), radius=1, colors=outer_colors,
       wedgeprops=dict(width=size, edgecolor='w'))
ax.pie(vals.flatten(), radius=1-size, colors=inner_colors,
       wedgeprops=dict(width=size, edgecolor='w'))
ax.set(aspect="equal", title='Pie plot with `ax.pie`')
plt.show()

在這里插入圖片描述

總結(jié)

本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!   

相關(guān)文章

  • pandas中.loc和.iloc以及.at和.iat的區(qū)別說(shuō)明

    pandas中.loc和.iloc以及.at和.iat的區(qū)別說(shuō)明

    這篇文章主要介紹了pandas中.loc和.iloc以及.at和.iat的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • pandas求兩個(gè)表格不相交的集合方法

    pandas求兩個(gè)表格不相交的集合方法

    今天小編就為大家分享一篇pandas求兩個(gè)表格不相交的集合方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • Python實(shí)現(xiàn)的括號(hào)匹配判斷功能示例

    Python實(shí)現(xiàn)的括號(hào)匹配判斷功能示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的括號(hào)匹配判斷功能,涉及Python棧與列表的存儲(chǔ)、遍歷、判斷等相關(guān)操作技巧,需要的朋友可以參考下
    2018-08-08
  • python?包實(shí)現(xiàn)JSON?輕量數(shù)據(jù)操作

    python?包實(shí)現(xiàn)JSON?輕量數(shù)據(jù)操作

    這篇文章主要介紹了python?包實(shí)現(xiàn)JSON?輕量數(shù)據(jù)操作,文章介紹內(nèi)容首先將對(duì)象轉(zhuǎn)為json字符串展開主題詳細(xì)內(nèi)容需要的小伙伴可以參考一下
    2022-04-04
  • Python混合使用同步和異步函數(shù)的方法

    Python混合使用同步和異步函數(shù)的方法

    Python是一種非常靈活的編程語(yǔ)言,可以混合使用同步和異步函數(shù)來(lái)實(shí)現(xiàn)更高效的編程。本文將介紹如何在Python中混合使用同步和異步函數(shù),以及如何在不同場(chǎng)景下選擇合適的函數(shù),感興趣的可以了解一下
    2023-03-03
  • Python中使用json.load()和json.loads()加載json數(shù)據(jù)的方法實(shí)例

    Python中使用json.load()和json.loads()加載json數(shù)據(jù)的方法實(shí)例

    在python編程中,我們經(jīng)常要用到j(luò)son對(duì)象作為數(shù)據(jù)交換格式,下面這篇文章主要給大家介紹了關(guān)于Python中使用json.load()和json.loads()加載json數(shù)據(jù)的方法實(shí)例,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • Python基于mysql實(shí)現(xiàn)學(xué)生管理系統(tǒng)

    Python基于mysql實(shí)現(xiàn)學(xué)生管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Python基于mysql實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • python實(shí)現(xiàn)將pvr格式轉(zhuǎn)換成pvr.ccz的方法

    python實(shí)現(xiàn)將pvr格式轉(zhuǎn)換成pvr.ccz的方法

    這篇文章主要介紹了python實(shí)現(xiàn)將pvr格式轉(zhuǎn)換成pvr.ccz的方法,涉及Python實(shí)現(xiàn)格式轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • 100行Python代碼實(shí)現(xiàn)自動(dòng)搶火車票(附源碼)

    100行Python代碼實(shí)現(xiàn)自動(dòng)搶火車票(附源碼)

    又到年底了,相信對(duì)于在外地的朋友們來(lái)說(shuō),火車票是到年底最頭痛的一件事了,但作為程序員的你怎么能一樣呢?快發(fā)揮你的特長(zhǎng),下面這篇文章主要給大家介紹了如果通過(guò)100行Python代碼實(shí)現(xiàn)自動(dòng)搶火車票的相關(guān)資料,需要的朋友可以參考下。
    2018-01-01
  • 利用Opencv實(shí)現(xiàn)圖片的油畫特效實(shí)例

    利用Opencv實(shí)現(xiàn)圖片的油畫特效實(shí)例

    這篇文章主要給大家介紹了關(guān)于利用Opencv實(shí)現(xiàn)圖片的油畫特效的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02

最新評(píng)論