佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 用python 繪制莖葉圖和復(fù)合餅圖

用python 繪制莖葉圖和復(fù)合餅圖

熱門標(biāo)簽:電話運營中心 企業(yè)做大做強 硅谷的囚徒呼叫中心 呼叫中心市場需求 Win7旗艦版 客戶服務(wù) 百度AI接口 語音系統(tǒng)

莖葉圖

from itertools import groupby
nums2=[225, 232,232,245,235,245,270,225,240,240,217,195,225,185,200,
    220,200,210,271,240,220,230,215,252,225,220,206,185,227,236]
for k, g in groupby(sorted(nums2), key=lambda x: int(x) // 10):
  print (k, list(g))
  # print('k', k)
  # print('g', list(g))
  lst = map(str, [int(y) % 10 for y in list(g)])
  print (k, '|', ' '.join(lst))

輸出:

18 | 5 5
19 | 5
20 | 0 0 6
21 | 0 5 7
22 | 0 0 0 5 5 5 5 7
23 | 0 2 2 5 6
24 | 0 0 0 5 5
25 | 2
27 | 0 1

說明:

1./ 就表示 浮點數(shù)除法,返回浮點結(jié)果; // 表示整數(shù)除法。

2.itertools.groupby 按照分組函數(shù)的值對元素進行分組。

>>> from itertools import groupby
>>> x = groupby(range(10), lambda x: x  5 or x > 8)
>>> for condition, numbers in x:
	print(condition, list(numbers))
輸出:
True [0, 1, 2, 3, 4]
False [5, 6, 7, 8]
True [9]

>>> [k for k, g in groupby('AAAABBBCCDAABBB')]
['A', 'B', 'C', 'D', 'A', 'B']
>>> [list(g) for k, g in groupby('AAAABBBCCD')]
[['A', 'A', 'A', 'A'], ['B', 'B', 'B'], ['C', 'C'], ['D']]

3.map(function, iterable, ...) 根據(jù)提供的函數(shù)對指定序列做映射。第一個參數(shù) function 以參數(shù)序列中的每一個元素調(diào)用 function 函數(shù),返回包含每次 function 函數(shù)返回值的新列表。
4.循環(huán)加處理的例子

>>> [int(y) % 10 for y in [22,73,34,92,45]]
[2, 3, 4, 2, 5]

復(fù)合餅圖

import numpy as np
import matplotlib as mpl
from matplotlib import cm
import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch

# 使圖表元素中正常顯示中文
mpl.rcParams['font.sans-serif'] = 'SimHei'
# 使坐標(biāo)軸刻度標(biāo)簽正常顯示負(fù)號
mpl.rcParams['axes.unicode_minus'] = False

#制畫布
fig = plt.figure(figsize=(9,5.0625), facecolor='cornsilk')
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

# 調(diào)整子區(qū)布局
fig.subplots_adjust(wspace=0)

# 大餅圖的制作
labels = ['成都','武漢','昆明','貴陽','西安','其它']
size = [802,530,477,256,233,307]
# 分裂距離
explode=(0,0,0,0,0,0.1)
ax1.pie(size,        # 數(shù)據(jù)
    autopct='%1.1f%%',  # 鍥形塊的數(shù)據(jù)標(biāo)簽格式
    startangle=30,    # 鍥形塊開始角度
    labels=labels,
    colors=cm.Blues(range(10, 300, 50)),
    explode=explode)

#小餅圖的制作
labels2 = ['西寧','拉薩','烏魯木齊','蘭州']
size2 = [102,79, 76, 50]
width=0.2
ax2.pie(size2,
    autopct='%1.1f%%',
    startangle=90,
    labels=labels2,
    colors=cm.Blues(range(10, 300, 50)),
    radius=0.5,
    shadow=False)

#使用ConnectionPatch畫出兩個餅圖的間連線
#先得到餅圖邊緣的數(shù)據(jù)
theta1, theta2 = ax1.patches[-1].theta1, ax1.patches[-1].theta2
center, r   = ax1.patches[-1].center, ax1.patches[-1].r
#畫出上邊緣的連線
x = r*np.cos(np.pi/180*theta2)+center[0]
y = np.sin(np.pi/180*theta2)+center[1]
con1 = ConnectionPatch(xyA=(0, 0.5),
            xyB=(x,y),
            coordsA=ax2.transData,
            coordsB=ax1.transData,
            axesA=ax2,axesB=ax1)
print(-width/2, 0.5)
print(x,y)

#畫出下邊緣的連線
x = r*np.cos(np.pi/180*theta1) + center[0]
y = np.sin(np.pi/180*theta1) + center[1]
con2 = ConnectionPatch(xyA=(-0.1, -0.49),
            xyB=(x,y),
            coordsA='data',
            coordsB='data',
            axesA=ax2,axesB=ax1)

# 添加連接線
for con in [con1, con2]:
  con.set_color('gray')
  ax2.add_artist(con)
  con.set_linewidth(1)

plt.show()

輸出:

以上就是用python 繪制莖葉圖和復(fù)合餅圖的詳細(xì)內(nèi)容,更多關(guān)于python 繪制莖葉圖和復(fù)合餅圖的資料請關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • 看看如何用Python繪制小米新版天價logo
  • Python繪制分類圖的方法
  • 用Python的繪圖庫(matplotlib)繪制小波能量譜
  • python opencv常用圖形繪制方法(線段、矩形、圓形、橢圓、文本)
  • python 繪制斜率圖進行對比分析
  • 通過python讀取txt文件和繪制柱形圖的實現(xiàn)代碼
  • python爬取股票最新數(shù)據(jù)并用excel繪制樹狀圖的示例
  • python繪制漢諾塔
  • 教你怎么用python繪制dotplot

標(biāo)簽:山西 海南 安康 濟南 山西 喀什 崇左 長沙

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《用python 繪制莖葉圖和復(fù)合餅圖》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    徐汇区| 将乐县| 民勤县| 日照市| 五河县| 青海省| 渭源县| 大新县| 轮台县| 龙川县| 嘉义县| 武汉市| 拜城县| 齐齐哈尔市| 五寨县| 长丰县| 丰都县| 芮城县| 寿阳县| 铜陵市| 丰顺县| 平安县| 甘谷县| 佛教| 论坛| 汾阳市| 晋江市| 武乡县| 永德县| 元谋县| 沂水县| 阿拉尔市| 仲巴县| 汨罗市| 右玉县| 凤庆县| 方山县| 纳雍县| 丰台区| 阳曲县| 江源县|