python画一些普通图的代码讲解

最近在学习可视化,我就统计了一下常见的图的画法;
下面关于一些普通的图的讲解:
下列的图包括:
普通柱状图;
并列柱状图;
叠加柱状图;
横向柱状图;
横向叠加柱状图;
折线图;
柱线图;
散点图;
直方图;
饼图;
双轴饼图;
盒图;
3D图;
绘制子图;

导入的一些包:

import numpy as np
import matplotlib.pyplot as plt
import  pandas as pd
from mpl_toolkits.mplot3d import Axes3D

一些数据准备

listy1 = [10,25,16,20,49,34,68,18,3,61]
listy21 = [26,66]
listy22 = [52,17]
listy3 = [3,6,7,12,1,9,15,18,20,10]
listy4 = [13,24,19,23,41,26,46,19,32,7]

listx1 = [1,2,3,4,5,6,7,8,9,10]
listx11 = [1101,1102,1103,1104,1105,1106,1107,1108,1109,1110]
listx2 = ["男","女"]

画前准备:

#防止汉字乱码
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#设置画布规格
plt.figure(figsize=(10,8))
#普通柱状图
#将数值放到柱状图上
for a,b in zip(listx1,listy1):
    plt.text(a,b+0.05,'%.0f'%b,ha='center',va='bottom',fontsize=11)
plt.bar(listx1,listy1,color=["b","g","r"])
plt.xlabel(u"普通柱状图X轴")
plt.ylabel(u"普通柱状图Y轴")
plt.xticks(listx1,listx11)  #换x坐标
plt.title("普通柱状图")
plt.show()

在这里插入图片描述
并列柱状图

width = 0.3  # 条形宽度
x = np.arange(len(listx2))
plt.bar(x,listy21,width=width,label=u'男',fc="g")
plt.bar(x + width,listy22,width=width,label=u'女',fc="r")
plt.xticks(x+width/2,listx2)           #替换横坐标的坐标
plt.title(u"并列柱状图")
plt.legend()                            #图例
plt.show()

在这里插入图片描述
#叠加柱状图

new = np.array(listy1)+np.array(listy3)#两个列表值相加

for a,b in zip(listx1,listy1):
    plt.text(a,b+0.05,'%.0f'%b,ha='center',va='bottom',fontsize=11)
for a,b,c in zip(listx1,listy3,listy1):
    plt.text(a,c+b+0.05,'%.0f'%b,ha='center',va='bottom',fontsize=11)
for a,b,c,d in zip(listx1,listy4,listy3,listy1):
    plt.text(a,c+d+b+0.05,'%.0f'%b,ha='center',va='bottom',fontsize=11)

plt.bar(listx1,listy1,label="下面的数",fc="g")
plt.bar(listx1,listy3,bottom=listy1,label="中间的数",fc="r")
plt.bar(listx1,listy4,bottom=new,label="上面的数",fc="y")
plt.xticks(listx1,listx11)
plt.title(u"叠加柱状图")
plt.legend()
plt.show()

在这里插入图片描述
横向柱状图

plt.barh(listx1,listy1,fc='b')
plt.title("横向柱状图")
plt.show()

在这里插入图片描述
横向叠加柱状图

new = np.array(listy1)+np.array(listy3)#两个列表值相加
plt.barh(listx1,listy1,label="下面的数",fc="g")
plt.barh(listx1,listy3,left=listy1,label="中间的数",fc="r")
plt.barh(listx1,listy4,left=new,label="上面的数",fc="y")
plt.yticks(listx1,listx11)
plt.title(u"横向叠加柱状图")
plt.legend()
plt.show()

在这里插入图片描述
折线图

plt.plot(listx1,listy1,label="11",color='red')
plt.plot(listx1,listy3,label="22",color='green')
plt.plot(listx1,listy4,label="33",color='yellow')
plt.title(u"折线图")
plt.legend()
plt.show()

在这里插入图片描述
#散点图

plt.plot(listx1,listy1,"r.",label="11")
plt.plot(listx1,listy3,"gp",label="22")
plt.plot(listx1,listy4,"yo",label="33")
#两种都可下面也可以
#plt.scatter(listx1,listy1,marker='o',c="red",label="1")
#plt.scatter(listx1,listy3,marker='.',c="green",label="2")
plt.title(u"散点图")
plt.legend()
plt.show()

在这里插入图片描述
#柱线图

plt.bar(listx1,listy1,label="11",fc='r')
plt.plot(listx1,listy3,label="22",color='g')
plt.title(u"柱线图")
plt.legend()
plt.show()

在这里插入图片描述
#普通饼图

plt.pie(listy1,labels=listx11,autopct='%1.2f%%') #画饼图(数据,数据对应的标签,百分数保留两位小数点)
plt.title(u"饼图")
plt.show()

在这里插入图片描述
#另一种饼图

expl = [0,0.2,0,0,0,0,0,0,0,0]   #第二块离开圆心0.2
colors  = ["blue","red","coral","green","yellow","orange"]  #设置颜色(循环显示)
plt.pie(listy1, explode=expl, colors=colors, labels=listx11,labeldistance = 1.2, autopct='%1.1f%%',pctdistance=0.8, shadow=True)#类似于labeldistance图例距圆心半径倍距离,pctdistance数值距圆心半径倍数距离
plt.title(u"另一种饼图")
plt.show()

在这里插入图片描述
#双组饼图

colors  = ["red","green"]
plt.pie(listy21,autopct="%3.1f%%",radius=0.7,pctdistance=0.75,colors=colors,textprops=dict(color="w"),wedgeprops=dict(width=0.3,edgecolor="w"))
plt.pie(listy22,autopct="%3.1f%%",radius=1,pctdistance=0.85,colors=colors,textprops=dict(color="w"),wedgeprops=dict(width=0.3,edgecolor="w"))
plt.legend(listx2)
plt.title("双组饼图")
plt.show()

在这里插入图片描述

# 直方图
#自己编的数据,效果不是很好

params=dict(histtype="stepfilled",alpha=0.3,normed=True,bins=10)#bins指定直方图中条块的个数
plt.hist(listy1,**params)
plt.hist(listy3,**params)
plt.hist(listy4,**params)
#plt.hist(listy1, # 指定绘图数据
#bins = 20, # 指定直方图中条块的个数
#color = 'steelblue', # 指定直方图的填充色
#edgecolor = 'black' # 指定直方图的边框色
#)
plt.title("直方图")
plt.show()

在这里插入图片描述

#盒图
list = [listy1,listy3,listy4]
labels=["1","2","3"]
plt.boxplot(list, labels=labels,notch=False, sym='o', vert=True,meanline=True)  #  第一个参数表示输入的列表, notch表示盒图的样子,sym表示偏离值的表示方法, vert表示竖着,还是横着,meanline表示是否显示均值线
plt.title("盒图")
plt.show()

在这里插入图片描述

#画z=50sin(x^2 + y^2)在x和y属于[-2,20],
fig = plt.figure()
ax = Axes3D(fig)
x = np.arange(-2,20,5)
y = np.arange(-2,20,5)
x,y=np.meshgrid(x,y)
z = 50 * np.sin((x**2+y**2))

ax.plot_surface(x,y,z,
                alpha=0.5,      #指定透明度
                rstride=1,      #指定行的跨度
                cstride=1,      #指定列的跨度
                cmap="winter"   #指定曲面块颜色映射
                )
ax.set_xlabel("X坐标轴")
ax.set_ylabel("Y坐标轴")
ax.set_zlabel("Z坐标轴")
plt.title(u"画3D曲线图")
plt.show()

在这里插入图片描述

#在一张图上绘制多个图形

ax1 = plt.subplot(2, 2, 1)
ax2 = plt.subplot(2, 2, 2)
ax3 = plt.subplot(2, 2, 3)
ax4 = plt.subplot(2, 2, 4)
plt.sca(ax1)
plt.bar(listx1,listy1,label="下面的数",fc="g")
plt.bar(listx1,listy3,bottom=listy1,label="上面的数",fc="r")
plt.legend()
plt.title(u"叠加柱状图")
plt.sca(ax2)
width = 0.3  # 条形宽度
x = np.arange(len(listx2))
plt.bar(x,listy21,width=width,label=u'男',fc="g")
plt.bar(x + width,listy22,width=width,label=u'女',fc="r")
plt.legend()
plt.title(u"并列柱状图")
plt.sca(ax3)
plt.bar(listx1,listy1,label="11",fc='r')
plt.plot(listx1,listy3,label="22",color='g')
plt.title(u"柱线图")
plt.legend()
plt.sca(ax4)
plt.plot(listx1,listy1,"r.",label="11")
plt.plot(listx1,listy3,"gp",label="22")
plt.plot(listx1,listy4,"yo",label="33")
plt.title(u"散点图")
plt.legend()

plt.show()

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值