拜师学习之Matplotlib(二)

1. 分两块绘制散点图

from matplotlib import pyplot as plt
y_3 = [11,12,13,14]
y_10 = [23,22,23,24]
x_3 = range(1,5)
x_10 = range(10,14)

plt.figure(figsize = (20,8) ,dpi=80)
plt.scatter(x_3,y_3)
plt.scatter(x_10,y_10)

_x = list(x_3)+list(x_10)
_x_labels = ["March {0}".format(i) for i in range(1,5)]
_x_labels += ["August {0}".format(i) for i in range(1,5)]
plt.xticks(_x[::3],_x_labels[::3],rotation=45)

2.绘制条形图

import matplotlib.pyplot as plt
import matplotlib
# 设置中文字体和负号正常显示
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False

label_list = ['2014', '2015', '2016', '2017']    # 横坐标刻度显示值
num_list1 = [20, 30, 15, 35]      # 纵坐标值1
num_list2 = [15, 30, 40, 20]      # 纵坐标值2
x = range(len(num_list1))
"""
绘制条形图
left:长条形中点横坐标
height:长条形高度
width:长条形宽度,默认值0.8
label:为后面设置legend准备
"""
rects1 = plt.bar(left=x, height=num_list1, width=0.4, alpha=0.8, color='red', label="一部门")
rects2 = plt.bar(left=[i + 0.4 for i in x], height=num_list2, width=0.4, color='green', label="二部门")
plt.ylim(0, 50)     # y轴取值范围
plt.ylabel("数量")
"""
设置x轴刻度显示值
参数一:中点坐标
参数二:显示值
"""
plt.xticks([index + 0.2 for index in x], label_list)
plt.xlabel("年份")
plt.title("某某公司")
plt.legend()     # 设置题注
# 编辑文本
for rect in rects1:
    height = rect.get_height()
    plt.text(rect.get_x() + rect.get_width() / 2, height+1, str(height), ha="center", va="bottom")
for rect in rects2:
    height = rect.get_height()
    plt.text(rect.get_x() + rect.get_width() / 2, height+1, str(height), ha="center", va="bottom")
plt.show()

3. 绘制多次条形图

from matplotlib import pyplot as plt
a = range(5)
b_1 = [11,12,13,14,15]
b_2 = [23,24,12,23,24]
b_3 = [34,12,13,13,42]

x_1 = list(a)
x_2 = [i+0.2 for i in range(5)]
x_3 = [i+0.4 for i in range(5)]
x_labels = ["1","2","3"]
plt.figure(figsize=(20,8),dpi=80)
plt.bar(x_1,b_1,width=0.2)
plt.bar(x_2,b_2,width=0.2]
plt.bar(x_3,b_3,width=0.2)

plt.xticks(x_2,x_lables)
plt.show()

3.绘制直方图

a = [12,13,14,14,14,15,16,16,16,15,15,16,17,14,15,14,14,12]

d = 2
num_bins = (max(a)-min(b))//d

plt.hist(a,num_bins)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值