完整的文章,更多的内容在我的新博客~戳我~
线图
plt.plot([1,2,3,4,5],
[1,4,9,16,25],
'--', c = 'r',linewidth=1) # 虚线 颜色。 ‘:’ 是点线, 设定线条宽度
plt.xlabel('xlabel',fontsize = 16) # 指定x y轴的标签
plt.ylabel('ylabel',fontsize = 16) # 设定标签的大小
x = np.linspace(-10,10) # 指定x轴的范围, 并返回一个数组
plt.plot(x, y, linewidth = 3.0) # 设置线条宽度
plt.plot(x, y, marker = 'o') # 显示关键点
plt.title('title') # 图的标题
plt.text(0,0, 'text') # 在图上某点写字
plt.grid(True) # 网格
绘制多个图
# 211 表示图是 2 行 1列 第 1 个图
plt.subplot(211)
plt.plot(x, y, color = 'r')
plt.subplot(212)
plt.plot(x, y, color = 'b')
柱状图
mean_values = [1,2,3] # 平均值
variance = [0.2, 0.4, 0.5] # 数据浮动值
bar_label = ['bar1', 'bar2', 'bar3']
x_pos = list(range(len(bar_label))) # x轴的坐标
plt.bar(x_pos, mean_values,yerr=variance) # 柱状图, yerr设置浮动值
max_y = max(zip(mean_values, variance))
plt.ylim(0, (max_y[0]+max_y[1])*1.2) # 设置y轴的范围
plt.xticks(x_pos, bar_label) # 设置x轴的坐标名
水平直方图
x1 = np.array([1,2,3])
x2 = np.array([2,2,3])
bar_labels = ['bar1', 'bar2', 'bar3']
fig = plt.figure(figsize = (8,6)) # 设置画布的大小
y_pos = np.arange(len(x1))
plt.barh(y_pos, x1) # 水平直方图
plt.barh(y_pos, -x2) # 可以向两个方向画
plt.xlim(-max(x2)-1, max(x1)+1) # 设置坐标轴的范围
plt.xlabel("YYY")
plt.yticks(y_pos, bar_labels)
fig, ax = plt.subplots(ncols = 2) # 画布分两列
ax[0].bar(x,y, color = 'red')
ax[1].barh(x,y,color = 'green')
ax[0].axhline(0) # 在 0 的位置加一条线,h横着
ax[1].axvline(0) # v 竖着
遍历图,改变特定柱的颜色
fig, ax = plt.subplots()
v = ax.bar(x, y, color = 'lightblue') # v 是所有柱的集合
for bar, height in zip(v, y): # 遍历条形图
if height < 0:
bar.set(color = 'red') # 改变特定柱的颜色
多个柱形图画在一起
pos = list(range(len(y1)))
w = 0.2 # 自定义柱的宽度
plt.bar(pos, y1, w, color = 'g')
plt.bar([p+w for p in pos], y2, w, color = 'b') # 新图往后一个w 宽度
plt.bar([p+w*2 for p in pos], y3, w, color = 'r')
在轴顶标数据
data = range(200, 225, 5)
bar_labels = ['a','b','c','d','e']
fig, ax = plt.subplots()
y_pos = np.arange(len(data))
plt.yticks(y_pos, bar_labels) # 设置y轴的标刻
bars = ax.barh(y_pos,data) # 画水平条形图
for b,d in zip(bars,data): # 表参数
plt.text(b.get_width(), b.get_y()+b.get_height()/2, '{:.2%}'.format(d/min(data)))
# b.get_width() 永远是横向的宽度,不论水平还是竖直的图
# b.get_height() 永远是条形图的高度,
# b.get_y() 一个柱所在的高度y
# {:.2%}'.format(d/min(data)) 格式化输出百分比
根据数据自动生成颜色风格
mean_values = range(10,18)
x_pos = range(len(mean_values))
import matplotlib.colors as col
import matplotlib.cm as cm
# cm.ScalarMappable(min,max,风格) 根据数据自动生成颜色
cmap1 = cm.ScalarMappable(col.Normalize(min(mean_values), max(mean_values), cm.hot))
cmap2 = cm.ScalarMappable(col.Normalize(0,20,cm.hot))
# 画图并指定cmap1格式的颜色
plt.bar(x_pos, mean_values, color=cmap1.to_rgba(mean_values))
填充画图
x = np.linspace(0,10,100)
y = np.sin(x)
plt.fill_between(x,y)
x = np.linspace(0,10,200)
y1 = 2*x + 1
y2 = 3*x +1.2
fig, ax = plt.subplots()
ax.fill_between(x, y1, y2, color = 'lightblue') # y1 y2 之间填充
不同模式的填充
patterns =('-','+','x','\\','*','o','O','.') # 各种填充样式
fig,ax = plt.subplots()
mean_value = range(1, len(patterns)+1)
x_pos = list(range(len(mean_value)))
bars = ax.bar(x_pos,mean_value, color='lightblue')
for b,p in zip(bars, patterns):
b.set_hatch(p) # 自己设置填充的样式
#b.set_hatch(pattern)