Sixteen, matplotlib charts

'' ' 
Matplotlib Python is a 2D drawing library. By Matplotlib, developers can require only a few lines of code,
can generate the drawing, histogram, power spectrum, bar, error, scatter and the like.
By learning Matplotlib, allowing data visualization, more intuitive truth to the user.
Make the data more objective, more persuasive. Matplotlib is a Python library, and is commonly used in the development of the library.
'' '
# Polyline drawing
# Import module matplotlib of pyplot module
Import matplotlib.pyplot AS PLT
X = [1,3,5,6,7]
Y = [1,4,9,16,23]
# draw
#plt .plot ([l, 3], [2,4])
plt.plot (X, Y)
# on display
plt.show ()

'''
绘制折线格式图形
'''
#导入模块
import matplotlib.pyplot as plt
#提供x,y的坐标
x = [2,5,7,9,13]
y = [4,6,8,34,67]
#进行绘制
plt.plot(x,y,linewidth=10) #绘制加粗
#设置x,y方向的展示
plt.xlabel('x')
plt.ylabel('y=x^2')
#设置一个字体,用来显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
#增加标题
plt.title("绘制y=x^2的图形")
#展示
plt.show()

'''绘制一元二次方程'''
import matplotlib.pyplot as plt
#x轴取数
x = range(-100,100)
y = [i**2 for i in x]
plt.plot(x,y)
#保存图片
plt.savefig("imags.png")
plt.show()

 

'''
绘制正玄余玄
'''
import matplotlib.pyplot as plt
import numpy as np
#x轴进行等分
x =np.linspace(0,10,100)
sin_y = np.sin(x)
cos_y = np.cos(x)
plt.plot(x,sin_y)
plt.plot(x,cos_y)
plt.show()
'''
对subplot的使用
'''
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1,10,100)
sin_y = np.sin(x)
plt.subplot(2,2,4) #调整画布,将图像画到第四象限
plt.xlim(-5,20) #x轴的大小
plt.ylim(-2,2) #y轴的大小
plt.plot(x,sin_y)
plt.show()

 

'''
在绘制散点图的时候,plot的绘制plt.plot(x,y_sin,'o')比scatter速度快,
但是如果散点图的大小和颜色不一样的时候,还是scatter比较快
'''
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0) #绘制多次,每次的点都是一样的
x = np.random.rand(100)
y = np.random.rand(100)
size = np.random.rand(10)*1000
color =np.random.rand(100)#随机生成100个颜色
plt.scatter(x,y,s=size,c=color,alpha=0.7)
plt.show()
 

 

'''
绘制柱状图
'''
import matplotlib.pyplot as plt
import numpy as np
x = [1994,1995,1996,1997]
x_lable = ['1994年','1995年','1996年','1997年']
y = [1000,3000,5000,6000]
plt.bar(x,y,width=0.5)
plt.rcParams['font.sans-serif'] = ['SimHei']
#修改x坐标的刻度
plt.xticks(x,x_lable)
#给x,y轴增加说明
plt.xlabel("年份")
plt.ylabel("销量")
plt.title("年销量关系图")
plt.show()

 

'''
给柱状同不同值添加颜色
'''
import matplotlib.pyplot as plt
import numpy as np


x = range(5)
y =np.random.randint(-5,5,5)
bars=plt.bar(x,y)
for bar,height in zip(bars,y): #zip就是一个拉链,一一对应
if height>0:
bar.set(color="red")
else:
bar.set(color="blue")
plt.show()

'''
统计男女比例
'''
import matplotlib.pyplot as plt
man =12344
woman =11443
man_proc =man/(man+woman)
woman_proc=woman/(man+woman)
#添加名称
plt.rcParams['font.sans-serif'] = ['SimHei']
labels=['男','女']
#添加颜色
colors=['red','blue']
paches,texts,autotext=plt.pie([man_proc,woman_proc],labels=labels,colors=colors,explode=(0,0.05),autopct='%0.1f%%')
#返回三个参数:texts饼图外部文本,autotext饼图内部文本
for autotextcolor in autotext:
autotextcolor.set_color('white')

for text in texts+autotext:
text.set_fontsize(20)
plt.show()

 

'''
统计三天三部电影的票房数
'''
import matplotlib.pyplot as plt
import numpy as np
width =0.3
ticket_name = ['千里千寻','玩具总动员','黑衣人:全球总动员']
ticket_num1 = [4523 ,1234,2345]
ticket_num2 = [2345,2354,4212]
ticket_num3 = [3333,5532,1235]
x =np.arange(len(ticket_name))
plt.bar(x,ticket_num1,width=width,alpha=0.5,label=ticket_name[0])
plt.bar([i+width for i in x],ticket_num2,width=width,alpha=0.5,label=ticket_name[1])
plt.bar([i+2*width for i in x],ticket_num3,width=width,alpha=0.5,label=ticket_name[2])
#设置x坐标 第1天、第2天、第3天
plt.rcParams['font.sans-serif'] = ['SimHei']
x_lable =['第{}天'.format(i+1) for i in x]
plt.xticks([i+width for i in x],x_lable)
plt.ylabel("票房数(万)")
#设置图例
plt.legend()
plt.title('统计三天的票房统计')
plt.show()

 

'''
绘制一个三维模式
'''
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#创建x,y,z 坐标
X =[1,1,2,2]
Y =[3,4,4,3]
Z =[12,56,2,23]
#创建figure
figure = plt.figure()
#创建Axes3D对象
axes3D =Axes3D(figure)
axes3D.plot_trisurf(X,Y,Z)
plt.show()


Guess you like

Origin www.cnblogs.com/dangjingwei/p/12353150.html