Python-matplotlib画图,柱状图,双柱图,折线图,散点图,饼图,线柱混合图。附代码模板与详细注释

自己整理注释的Python画图合集,包含常见的各种画图类型。

1.Python 饼状图

import numpy as np	# 加载数学库用于函数描述
import matplotlib.pyplot as plt

 ###########画ax1  ax1为单柱状图 ################

# # 饼状图
# plot.figure(figsize=(8,8))
labels = ['Canteen', 'Supermarket', 'Dorm', 'Others']
sizes = [70, 20, 44, 2]  #各个部分的数目 自动计算百分比例
colors = ['r', 'yellow', 'b', 'green']

explode = (0.05, 0, 0, 0)#各个部分离圆心距离

patches, l_text, p_text = plt.pie(sizes, explode=explode, labels=labels, colors=colors,
                                    labeldistance=1.1, autopct='%2.0f%%', shadow=True,
                                    startangle=90, pctdistance=0.6)

# labeldistance,label的位置离远点有多远,1.1指1.1倍半径的位置
# autopct,圆里面的文本格式,%3.1f%%表示小数有三位,整数有一位的浮点数
# shadow,饼是否有阴影
# startangle,起始角度,0,表示从0开始逆时针转,为第一块。一般选择从90度开始比较好看
# pctdistance,百分比的text离圆心的距离
# patches, l_texts, p_texts,为了得到饼图的返回值,p_texts饼图内部文本的,l_texts饼图外label的文本

# 改变文本的大小
# 方法是把每一个text遍历。调用set_size方法设置它的属性
for t in l_text:
    t.set_size = 30
for t in p_text:
    t.set_size = 20
# 设置x,y轴刻度一致,这样饼图才能是圆的
plt.axis('equal')
plt.legend(loc='upper left', bbox_to_anchor=(-0.1, 1))
# loc: 表示legend的位置,包括'upper right','upper left','lower right','lower left'等
# bbox_to_anchor: 表示legend距离图形之间的距离,当出现图形与legend重叠时,可使用bbox_to_anchor进行调整legend的位置
# 由两个参数决定,第一个参数为legend距离左边的距离,第二个参数为距离下面的距离
#plt.grid()
plt.show()

效果如图:

 2. Python 纯柱状图,单柱与双柱图,柱状图与结合散点图 

import numpy as np	# 加载数学库用于函数描述
import matplotlib.pyplot as plt

x=np.arange(0,10,1)#start stop step   数据
y=x*x+1.5
y1=100-y

 ###########画ax1  ax1为单柱状图 ################

fig=plt.figure(1)        #新建画布1

ax1=fig.add_subplot(2,2,1)      #定义子画布 以及位置

ax1.set_title("this is a title for ax1")      #主标题
ax1.set_ylabel("y lable",c="b")     #y左标题
ax1.set_xlabel("x lable")     #x标题
ax1.set_xlim(0,10)    #x坐标范围
ax1.set_ylim(0,100)

ax1.bar(x, y, color="b", alpha=1, label='a')#画散点   alpha 颜色深度
ax1.legend()    #添加标签

  ###########画ax2 曲线图  与ax1共用x轴 ################
ax2=ax1.twinx()  #共用X轴 twiny

ax2.plot(x,y1,c="g",label="a2") #点画线
ax2.set_ylabel("y1 label",c="g")
ax2.set_ylim(0,108)
plt.legend()

###########画ax3 两个对象的柱状图 ################
fig1=plt.figure(1) 
ax3=fig1.add_subplot(2,2,4)  #定义字画布位置

xtick=("a1","a2","a3","a4","a5","a6","a7","a8","a9","a10")#自己定义x坐标显示
wi=1
plt.xticks(3*x+wi/2,xtick)
ax3.bar(3*x, height=y,width=1,color="r",label=u'superpoint/s')
ax3.bar(3*x+wi,height= y1,width=1,color="b",label=u'SIFT/s')
ax3.legend()#画布3标签

plt.show()

   效果如图:

3.Python 纯散点图  多类型散点 折线图

import numpy as np	# 加载数学库用于函数描述
import matplotlib.pyplot as plt

x=np.arange(0,10,1)#start stop step   数据
y=x*x+1.5
y1=100-y

 ###########画ax1  ax1为散点图 ################

fig=plt.figure(1)        #新建画布1

ax1=fig.add_subplot(2,2,1)      #定义子画布 以及位置

ax1.set_title("this is a title for ax1")      #主标题
ax1.set_ylabel("y lable",c="b")     #y左标题
ax1.set_xlabel("x lable")     #x标题
ax1.set_xlim(0,10)    #x坐标范围
ax1.set_ylim(0,100)

ax1.scatter(x, y, s=1, c="b", alpha=0.1, label='a')#画散点   alpha 颜色深度
ax1.legend()    #添加标签
# loc: 表示legend的位置,包括'upper right','upper left','lower right','lower left'等
  ###########画ax2 曲线图  与ax1共用x轴 ################
ax2=ax1.twinx()  #共用X轴 twiny

ax2.plot(x,y1,c="g",label="a2") #点画线
ax2.set_ylabel("y1 label",c="g")
ax2.set_ylim(0,108)
ax2.legend()

###########画ax3 点线共存图  另开字画部 ################
fig1=plt.figure(1) 
ax3=fig1.add_subplot(2,2,4)
ax3.plot(x, y,'o-',c="r",label=u'superpoint/s')
ax3.legend()
plt.show()

效果如图:(子图1类型a是稀疏蓝点 有点看不清楚)

猜你喜欢

转载自blog.csdn.net/weixin_48464886/article/details/119796769
今日推荐