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

时间秒杀一切 提交于 2020-08-12 01:06:38

图3.1

 

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=[1,2,3,4,5]
y=[6,10,4,5,1]

plt.grid(True, axis='y',ls=':',color='r',alpha=0.3)

plt.bar(x,y,align='center', color='b', tick_label=['A','B','C','D','E'],
        alpha=0.6, edgecolor="black")

plt.xlabel('测试难度')
plt.ylabel('试卷份数')

plt.show()
View Code

 

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

 

图3.2

 

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=[1,2,3,4,5]
y=[6,10,4,5,1]

plt.grid(True, axis='x',ls=':',color='r',alpha=0.3)

plt.barh(x,y,align='center', color='c', tick_label=['A','B','C','D','E'],
        alpha=0.6, edgecolor="black")

plt.ylabel('测试难度')
plt.xlabel('试卷份数')

plt.show()
View Code

 

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

 

图 3.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 

x=[1,2,3,4,5]
y=[6,10,4,5,1]
y1=[2,6,3,8,5]

plt.bar(x,y,align='center',color='#66c2a5', tick_label=['A','B','C','D','E'],
        label='班级A', edgecolor='black')

plt.bar(x,y1,align='center',color='#8da0cb', bottom=y, 
        label='班级B', edgecolor='black')

plt.xlabel("测试难度")
plt.ylabel("测试份数")

plt.legend()

plt.show()
View Code

 

 

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

 

图 3.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 

x=[1,2,3,4,5]
y=[6,10,4,5,1]
y1=[2,6,3,8,5]

plt.barh(x,y,align='center',color='#66c2a5', tick_label=['A','B','C','D','E'],
        label='班级A', edgecolor='black')

plt.barh(x,y1,align='center',color='#8da0cb', left=y, 
        label='班级B', edgecolor='black')

plt.ylabel("测试难度")
plt.xlabel("测试份数")

plt.legend()

plt.show()
View Code

 

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

 

图 3.5

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.array([1,2,3,4,5])
y=[6,10,4,5,1]
y1=[2,6,3,8,5]


bar_width=0.35
tick_label=['A','B','C','D','E']


plt.bar(x, y, bar_width, align='center',color='c', label='班级A', alpha=0.5)
plt.bar(x+bar_width,y1,bar_width, align='center',color='b', label='班级B', alpha=0.5)

plt.xticks(x+bar_width/2, tick_label)
 
plt.xlabel("测试难度")
plt.ylabel("试卷份数")

plt.legend()

plt.show()
View Code

 

 

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

 

图 3.6

 

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.array([1,2,3,4,5])
y=[6,10,4,5,1]
y1=[2,6,3,8,5]


bar_width=0.35
tick_label=['A','B','C','D','E']


plt.barh(x, y, bar_width, align='center',color='c', label='班级A', alpha=0.5)
plt.barh(x+bar_width,y1,bar_width, align='center',color='b', label='班级B', alpha=0.5)

plt.yticks(x+bar_width/2, tick_label)
 
plt.ylabel("测试难度")
plt.xlabel("试卷份数")

plt.legend()

plt.show()
View Code

 

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

 

图 3.7

 

 

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=[1,2,3,4,5]
y=[6,10,4,5,1]


plt.bar(x,y, align='center', color='c', tick_label=['A','B','C','D','E'], 
        hatch='///')


plt.xlabel("测试难度")
plt.ylabel("试卷份数")

plt.show()
View Code

 

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

 

图 3.8

 

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

x=np.arange(1,6,1)
y=[0,4,3,5,6]
y1=[1,3,4,2,7]
y2=[1,1,1,1,1]

labels=['BluePlanet', 'BrownPlanet', 'GreenPlanet']
colors=['#8da0cb','#fc8d62','#66c2a5']

plt.stackplot(x, y, y1, y2, labels=labels, colors=colors)

plt.legend(loc='upper left')

plt.show()
View Code

 

 

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

 

图 3.9

 

 

 

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 


plt.broken_barh([(30,100),(180,50),(260,70)], (20,8), facecolors='#1f78b4')
plt.broken_barh([(60,90),(190,20),(230,30),(280,60)], (10,8), 
                facecolors=['#7fc97f','#beaed4','#fdc086','#ffff99'])

plt.xticks(np.arange(0,361,60))
plt.yticks([15,25],['歌剧院A','歌剧院B'])

plt.xlim(0, 360)
plt.ylim(5, 35)

plt.xlabel("演出时间(分)")
plt.grid(ls='-', lw=1, color='gray')

plt.title("不同地区的歌剧院的演出时间比较")

plt.show()
View Code

 

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

 

图 3.10

 

 

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.linspace(1,10,10)
y=np.sin(x)


plt.step(x,y,color='#8dd3c7', where='pre', lw=2)


plt.xlim(0, 11)
plt.ylim(-1.2, 1.2)

plt.xticks(np.arange(1, 11, 1))

plt.show()
View Code

 

 

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

 

图 3.11

 

 

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.linspace(1,10,10)
y=np.sin(x)


plt.step(x,y,color='#8dd3c7', where='post', lw=2)


plt.xlim(0, 11)
plt.ylim(-1.2, 1.2)

plt.xticks(np.arange(1, 11, 1))

plt.show()
View Code

 

 

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

 

图 3.12

 

 

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 

scoresT=np.random.randint(0,100,100)

x=scoresT

bins=range(0,101,10)

plt.hist(x, bins, color='#377eb8', histtype='bar',rwidth=1.0, edgecolor="black")


plt.xlabel("测试成绩")
plt.ylabel("学生人数")

plt.show()
View Code

 

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

 

图 3.13

 

 

 

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

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!