matplotlib

# 一个图像处理包

Matplotlib中的基本图表包括的元素

-x轴和y轴 axis
  水平和垂直的轴线
-x轴和y轴刻度 tick
  刻度标示坐标轴的分隔,包括最小刻度和最大刻度
-x轴和y轴刻度标签 tick label
  表示特定坐标轴的值
-绘图区域(坐标系) axes
  实际绘图的区域
-坐标系标题 title
  实际绘图的区域
-轴标签 xlabel ylabel
  实际绘图的区域
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import Series,DataFrame
#一条直线
x=[1,2,3,4,5]
y=[2,4,6,8,10]
plt.plot(x,y) #线性之线

x = np.linspace(-3,3,8) #start end num
y = x**2
plt.plot(x,y)  #抛物线

x = x
y = np.sin(x)
plt.plot(x,y)  #正余弦曲线
1 连续调多次plot
plt.plot(x,y) 
plt.plot(x+2,y-1) 
2 一个plot函数传多组xy
plt.plot(x,y,x+2,y-1) 
包含多个曲线的图
将多个曲线图绘制在一个table区域中:对象形式创建表图
a=plt.subplot(row,col,loc) 创建曲线图
a.plot(x,y) 绘制曲线图

plt.subplot(2,2,1) #2*2的表格
plt.plot(x,y) 

plt.subplot(2,2,2) 
plt.plot(x+1,y-3)

plt.subplot(2,2,3) 
plt.plot(x+5,y+2)

plt.subplot(2,2,4)  
plt.plot(x-1,y-5)
绘制数据在表格里面
plt.plot(x,y)
plt.axis([-6,6,-2,2]) #axis方法:设置x,y轴刻度值的范围  [xmin xmax ymin ymax]   修改可读范围
plt.axis('off') # 坐标轴显示消失
坐标轴修改
设置画布比例:plt.figure(figsize=(a,b)) a:x刻度比例 b:y刻度比例 (2:1)表示x刻度显示为y刻度显示的2倍
plt.figure(figsize=(10,5))
plt.plot(x,y)
画布比例调整
s 标签内容
color 标签颜色
fontsize 字体大小
rotation 旋转角度

plt.plot(x,y)
plt.xlabel('xx')
plt.ylabel('yy')
plt.title('tt')
坐标轴标签
plt.plot(x,y,label='AA')
plt.plot(x+3,y-4,label='BB')
plt.legend() #每条线所表示的含义
图例 legend ncol列数 loc位置 best最优位置
保存图片 fig 
plt.plot(x,y,c='red',alpha=0.7,ls=':',maker='h',makersize=10) #color 透明度  ls线型 lw线宽 
点型 maker 
color maker

# 2D图形

salary = np.array([12345,10000,15000,18000,20000,15555,10050,19999,12000,12500])
# qu = [10000,12000,15000,18000,20000]
plt.hist(salary)   
#bins直方图个数 ,默认10 
#color 颜色
#orientation  通过设置orientation为horizontal创建水平直方图。默认值为vertical
直方图 -- 是一个特殊的柱状图,密度图
找出两组不相关数据间的规律
x = [33,35,34,31,36]     #沿海城市的温度
y = [100,200,150,166,177]  #距离海洋的举例 海里
plt.scatter(x,y)

x = np.linspace(10,20,num=30)  #等差数值 
y = np.random.randint(10,20,size=(30,))  
# plt.scatter(x,y,c='rbgy')  #散开  red blue grey yellow
散点图 因变量随自变量而变化的大致趋势
饼图阴影、分裂等属性设置
#labels参数设置每一块的标签;
#labeldistance参数设置标签距离圆心的距离(比例值)
#autopct参数设置比例值小数保留位(%.3f%%);
#pctdistance参数设置比例值文字距离圆心的距离
#explode参数设置每一块顶点距圆心的长度(比例值,列表);
#colors参数设置每一块的颜色(列表);
#shadow参数为布尔值,设置是否绘制阴影
#startangle参数设置饼图起始角度

arr = [0.2,0.3,0.1,0.2]
plt.pie(arr,labels=['a','b','c','d'])

arr=[11,22,31,15]
plt.pie(arr,labels=['a','b','c','d'])

#labeldistance参数设置标签距离圆心的距离(比例值)
arr=[11,22,31,15]
plt.pie(arr,labels=['a','b','c','d'],labeldistance=0.3)

#autopct参数设置比例值小数保留位(%.3f%%);
arr=[11,22,31,15]
plt.pie(arr,labels=['a','b','c','d'],labeldistance=0.3,autopct='%.6f%%')

##explode参数设置每一块顶点距圆心的长度(比例值,列表);
arr=[11,22,31,15]
plt.pie(arr,labels=['a','b','c','d'],labeldistance=0.3,shadow=True,explode=[0.2,0.3,0.2,0.4])

#startangle参数设置饼图起始角度
arr=[11,22,31,15]
plt.pie(arr,labels=['a','b','c','d'],startangle=50)  #旋转角度
饼图
条形图:plt.bar()
参数:第一个参数是索引。第二个参数是数据值。第三个参数是条形的宽度
-【条形图有两个参数x,y】

width 纵向设置条形宽度
height 横向设置条形高度
bar()、barh()

x = [1,2,3,4,5]
y = [6,7,8,9,10]
plt.barh(x,y)  #横向
plt.bar(x,y)  #竖向
条形图

 

posted @ 2019-07-04 10:02  追风zz  阅读(202)  评论(0编辑  收藏  举报