编程语言
首页 > 编程语言> > Python数据可视化之matplotlib实践 源码 第一篇 入门 第二章

Python数据可视化之matplotlib实践 源码 第一篇 入门 第二章

作者:互联网

图 2.1

 

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False

x=[1,2,3,4,5,6,7,8]
y=[3,1,4,5,8,9,7,2]

plt.bar(x, y, align='center',color='c', tick_label=['q','a','c','e','r',
                'j','b', 'p'], hatch='/')

plt.xlabel('箱子编号')
plt.ylabel('箱子重量(kg)')

plt.show()
View Code

 

 

===================================================

 

 图 2.2

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False

x=[1,2,3,4,5,6,7,8]
y=[3,1,4,5,8,9,7,2]

plt.barh(x, y, align='center',color='c', tick_label=['q','a','c','e','r',
                'j','b', 'p'], hatch='/')

plt.ylabel('箱子编号')
plt.xlabel('箱子重量(kg)')

plt.show()
View Code

 

===================================================

 

图  2.3

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False 

boxWeight=np.random.randint(0,10,100)

x=boxWeight

bins=range(0,11,1)

plt.hist(x, bins=bins, color='g', histtype='bar', rwidth=1, alpha=0.6, edgecolor='black')

plt.xlabel('箱子重量 (kg)')
plt.ylabel('销售数量 (个)')

plt.show()
View Code

 

===================================================

 

图   2.4

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False 

kinds=['简易箱','保温箱','行李箱','密封箱']

colors=['#e41a1c', '#377eb8', '#4daf4a', '#984ea3']

soldNums=[0.05, 0.45, 0.15, 0.35]

plt.pie(soldNums, labels=kinds, autopct='%3.1f%%', startangle=60, colors=colors)

plt.title('不同箱子类型的销售数量占比')

plt.show()
View Code

 

 

===================================================

 

标签:plt,show,Python,mpl,matplotlib,源码,rcParams,import
来源: https://www.cnblogs.com/devilmaycry812839668/p/12887897.html