《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

 

 

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

 

图 2.5

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

barSlices=18

theta=np.linspace(0.0, 2*np.pi, barSlices, endpoint=False)

r=30*np.random.rand(barSlices)

plt.polar(theta, r, color='chartreuse', linewidth=2, marker='*', mfc='b', ms=10)

plt.show()
View Code

 

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

 

图 2.6

 

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

a=np.random.randn(100)
b=np.random.randn(100)

plt.scatter(a, b, s=np.power(10*a+20*b,2), 
            c=np.random.rand(100), cmap=mpl.cm.RdYlBu,marker='o')

plt.show()
View Code

 

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

 

图 2.7

 

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

x=np.linspace(0.5, 2*np.pi, 20)
y=np.random.randn(20)

plt.stem(x,y,linefmt='-.', markerfmt='*', basefmt='-')

plt.show()
View Code

 

 

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

 

图 2.8

 

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 

x=np.random.randn(1000)

plt.boxplot(x)

plt.xticks([1], ['随机数生成器AlphaRM'])

plt.ylabel("随机数值")

plt.title("随机数生成器抗干扰能力的稳定性")

plt.grid(axis='y', ls=':', lw=1, color='gray', alpha=0.4)

plt.show()
View Code

 

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

 

图 2.9

 

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

x=np.linspace(0.1, 0.6, 6)

y=np.exp(x)

plt.errorbar(x, y, fmt='bo:', yerr=0.2, xerr=0.02)

plt.xlim(0, 0.7)

plt.show()
View Code

 

posted on 2020-05-14 12:42  Angry_Panda  阅读(628)  评论(0编辑  收藏  举报

导航