python一般画图方法

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
显示中文
mpl.rcParams[‘font.sans-serif’] = [‘SimHei’]
mpl.rcParams[‘axes.unicode_minus’] = False
a[‘b’]= pd.to_datetime(a[‘b’]) 转换时间

折线图
plt.plot(x,y,ls=,lw=,c=,marker=,markersize=,markeredgecolor=,markerfacecolor, label=)
x: x轴上的数值
y: y轴上的数值
ls: 折线的风格( ‘-‘, ’–‘, ’-.‘和’:‘)
lw: 线条宽度
c: 颜色
marker: 线条上点的形状
markersize: 线条上点的形状
markeredgecolor: 点的边框色
markerfacecolor: 点的填充色
label: 文本标签
plt.plot(a[‘b’],a[‘c’],linestyle = ‘-’, linewidth = 2, color = ‘steelblue’, marker = ‘o’,
markersize = 6, markeredgecolor=‘black’, markerfacecolor=‘brown’)
不同类别折线图在一起显示
plt.plot(a[‘b’],a[‘c’],‘bs-’,a[‘b’],a[‘d’],‘ro-.’)

plt.plot(jd_stock[‘opening_price’],label=‘Opening_Price’)
plt.plot(jd_stock[‘closing_price’],label=‘Closing_Price’)

饼状图
plt.pie(x, explode,labels,colors,autopct=,pctdistance,shadow,startangle,radius,
wedgeprops,textprops,center)
x:数据
–labels: 标签 --counterclock: 是否逆时针呈现:
–colors:颜色 --wedgeprops: 设置饼图内外边界的属性
–autopct: 百分比 --textprops: 设置饼图中文本属性
–pctdistance: 百分比标签与圆心距离 --center: 设置中心位置
–shadow: 是否添加饼图阴影效果
–labeldistance: 设置各扇形标签与圆心距离
–startangle: 设置饼图的初始摆放角度
–radius: 设置饼图半径大小

labels =[“A难度水平”,‘B难度水平’,‘C难度水平’,‘D难度水平’]
students = [0.35,0.15,0.20,0.30]
colors = [‘red’,‘green’,‘blue’,‘yellow’]
explode = (0.1,0.1,0,0)
plt.pie(a[‘b’],explode = explode,labels =labels,autopct=’%3.2f%%’,startangle=45,shadow=True,
colors=colors)

柱状图
plt.bar(x, y,width,bottom,color,linewidth,tick_label,align)
• x: 指定x轴上数值
• y: 指定y轴上数值
• width: 指定条形图宽度
• color: 条形图的填充色
• edge: 条形图的边框色
• bottom: 百分比标签与圆心距离
• linewidth: 条形图边框宽度
• tick_label: 条形图的刻度标签
• align: 指定x轴上对齐方式
labels = [‘q’,‘a’,‘c’,‘e’,‘r’,‘j’,‘b’,‘p’]
plt.bar(a[‘b’],a[‘c’],align=‘center’,color=‘y’,tick_label=labels,hatch=’-’)

industry_GDP = pd.read_excel(‘Industry_GDP.xlsx’)
temp = pd.crosstab(industry_GDP[‘Quarter’],industry_GDP[‘Industry_Type’],values=industry_GDP[‘GDP’],aggfunc=np.sum)
plt.bar(x= temp.index.values,height= temp[‘第一产业’],color=‘steelblue’,label=‘第一产业’,tick_label = temp.index.values)
plt.bar(x= temp.index.values,height= temp[‘第二产业’],bottom =temp[‘第一产业’], color=‘green’,label=‘第二产业’,tick_label = temp.index.values)
plt.bar(x= temp.index.values,height = temp[‘第三产业’],bottom =temp[‘第一产业’] + temp[‘第二产业’],color=‘red’,label=‘第三产业’,tick_label = temp.index.values)

temp = pd.crosstab(industry_GDP[‘Quarter’],industry_GDP[‘Industry_Type’],values=industry_GDP[‘GDP’],aggfunc=np.sum)
bar_width = 0.2 #设置宽度
quarter = temp.index.values #取出季度名称
plt.bar(x= np.arange(0,4),height= temp[‘第一产业’],color=‘steelblue’,label=‘第一产业’,width = bar_width)
plt.bar(x= np.arange(0,4) + bar_width,height= temp[‘第二产业’], color=‘green’,label=‘第二产业’,width=bar_width)
plt.bar(x= np.arange(0,4) + 2*bar_width,height= temp[‘第三产业’], color=‘red’,label=‘第三产业’,width=bar_width)

直方图
plt.hist(x,bins,range,normed,cumulative,bottom,align,rwidth,color,edgecolor,label)
•x: 数据
•bin: 条形个数
•range: 上下界
•normed: 是否将频数转换成频率
•cumulative: 是否计算累计频率
•bottom: 为直方图的每个条形添加基准线,默认为0
•align: 对齐方式
•rwidth: 条形的宽度
•color: 填充色
•edgecolor: 设置直方图边框色
•label: 设置直方图标签

plt.hist(a[‘b’], bins=20,color=‘c’,edgecolor =‘black’,density=True,normed = True)
a[‘b’].plot(kind=‘kde’,color=‘red’,label=‘核密度图’)

散点图
plt.scatter(x,y,s,c,marker,cmap,norm,alpha,linewidths,edgecolorsl)
•x: x数据
•y: y轴数据
•s: 散点大小
•c: 散点颜色
•marker: 散点图形状
•cmap: 指定某个colormap值,该参数一般不用,用默认值
•norm: 设置数据亮度
•alpha: 散点的透明度
•linewidths: 散点边界线的宽度
•edgecolors: 设置散点边界线的颜色
plt.scatter(x = iris.Petal_Width,y = iris.Petal_Length,s =10,color =‘steelblue’)

不同种类的散点图关系
plt.scatter(x = iris.Petal_Width[iris[‘Species’] ==‘setosa’],
y = iris.Petal_Length[iris[‘Species’] ==‘setosa’],
s =20,color =‘steelblue’,marker=‘o’,label = ‘setosa’,alpha=0.8)
plt.scatter(x = iris.Petal_Width[iris[‘Species’] ==‘versicolor’],
y = iris.Petal_Length[iris[‘Species’] ==‘versicolor’],
s =30,color =‘indianred’,marker=‘s’,label = ‘versicolor’)
plt.scatter(x = iris.Petal_Width[iris[‘Species’] ==‘virginica’],
y = iris.Petal_Length[iris[‘Species’] ==‘virginica’],
s =40,color =‘green’,marker=‘x’,label = ‘virginica’)

使用循环方式
colors_iris = [‘steelblue’,‘indianred’,‘green’]
sepcies =[ ‘setosa’,‘versicolor’,‘virginica’]
merker_iris =[‘o’,‘s’,‘x’]
for i in range(0,3):
plt.scatter(x=iris.Petal_Width[iris[‘Species’] ==sepcies[i]], y=iris.Petal_Length[iris[‘Species’] == sepcies[i]], s=20,color=colors_iris[i], marker=merker_iris[i], label=sepcies[i])

箱线图
plt.boxplot(x,notch,sym,vert,whis,positions,widths,patch_artist,meanline,showmeans,
boxprops,labels,flierprops
•x: 数据
•width:宽度
•patch_artist: 是否填充箱体颜色
•meanline:是否显示均值
•showmeans: 是否显示均值
•meanprops;设置均值属性,如点的大小,颜色等
•medianprops:设置中位数的属性,如线的类型,大小等
•showfliers: 是否表示有异常值
•boxprops:设置箱体的属性,边框色和填充色
•cappops: 设置箱线顶端和末端线条的属性,如颜色,粗细等

plt.boxplot(x=a[‘b’],patch_artist=True,showmeans =True,
boxprops={‘color’:‘black’,‘facecolor’:‘steelblue’},showfliers=True,
flierprops={‘marker’:‘o’,‘markerfacecolor’:‘red’,‘markersize’:3},
meanprops={‘marker’:‘D’,‘markerfacecolor’:‘indianred’,‘markersize’:4},
medianprops={‘linestyle’:’–’,‘color’:‘orange’},labels=[’’])

猜你喜欢

转载自blog.csdn.net/bigdata_zx/article/details/84110150