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]
num_list2 = [15, 30, 40, 20]
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)
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)