- 设置图例
- 设置注解
- 设置坐标轴可见度
- 散点图
- 柱状图
- 等高线
设置图例
import numpy as np
from matplotlib import pyplot as plt
'此块已展示,直接看下面'
x = np.linspace(-3, 3, 50)
y1 = 2 * x + 1
y2 = x ** 2
plt.figure(num=3, figsize=(8, 5))
'坐标轴取值范围'
plt.xlim((-1, 2))
plt.ylim((-2, 3))
'坐标轴取别名'
plt.xlabel('x axis')
plt.ylabel('y axis')
'更换x轴信息'
new_ticks = np.linspace(-1, 2, 5)
plt.xticks(new_ticks)
plt.yticks(
[-2, -1.8, -1, 1.22, 3],
[r'$really\ bad$', r'$bad\ \alpha$', r'$normal$', r'$good$', r'$really\ good$']
)
'设置图例'
l1, =plt.plot(x, y2, label='up')
l2, = plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--', label='down')
plt.legend(handles=[l1, l2], labels=['aaa', 'bbb'], loc='best')
plt.show()
设置注解
import numpy as np
from matplotlib import pyplot as plt
'此块已展示,直接看下面'
x = np.linspace(-3, 3, 50)
y = 2 * x + 1
plt.figure(num=1, figsize=(8, 5))
plt.plot(x, y)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
'注解'
x0 = 1
y0 = 2 * x0 + 1
# 点点
plt.scatter(x0, y0, s=50, color='b')
# 做垂线
plt.plot([x0, x0], [y0, 0], 'k--', lw=2.5)
# 指向某点的注解
plt.annotate(r'$2x+1=%s$' % y0, xy=(x0, y0), xycoords='data', xytext=(+30, -30),
textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle='->', connectionstyle='arc3, rad=.2'))
# 文字形式的注解
plt.text(-3.7, 3, r'$This\ is\ the\ some\ text.\ \mu\ \sigma_i\ \alpha_t $',
fontdict={'size': 16, 'color': 'r'})
plt.show()
设置坐标轴可见度
import numpy as np
from matplotlib import pyplot as plt
'线条展示'
x = np.linspace(-3, 3, 50)
y = 0.1 * x
plt.figure()
# 特别注意 zorder 需要设置
plt.plot(x, y, linewidth=10, zorder=1)
# 设置y的取值范围
plt.ylim(-2, 2)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
'设置坐标轴可见度'
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(12)
label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.7))
label.set_zorder(100)
plt.show()
散点图
import numpy as np
from matplotlib import pyplot as plt
# 设置数据点数量
n = 1024
# 随机正态分布
x = np.random.normal(0, 1, n)
y = np.random.normal(0, 1, n)
# 设置颜色值
t = np.arctan2(y, x)
# 散点图,alpha是透明度,s=size,c=color(简略散点图需要注释)
plt.scatter(x, y, s=75, c=t, alpha=0.5)
# 设置参数范围(简略散点图需要注释)
plt.xlim((-1.5, 1.5))
plt.ylim((-1.5, 1.5))
## 将上述两项注释则得简略散点图
#plt.scatter(np.arange(5), np.arange(5))
# 设置坐标轴信息可见性,空括号表示不显示
plt.xticks(())
plt.yticks(())
plt.show()
简略散点图
柱状图
import numpy as np
from matplotlib import pyplot as plt
# 设置数据点数量
n = 12
x = np.arange(n)
# 均匀分布
y1 = (1 - x / float(n)) * np.random.uniform(0.5, 1.0, n)
y2 = (1 - x / float(n)) * np.random.uniform(0.5, 1.0, n)
# 生成特定形式柱状图
plt.bar(x, +y1, facecolor='#9999ff', edgecolor='white')
plt.bar(x, -y2, facecolor='#ff9999', edgecolor='white')
x1 = x
x2 = x
# 对第一个(向上的那个)柱状图添加数字
for x, y in zip(x1, y1):
plt.text(x, y + 0.05, '%.2f' % y, ha='center', va='bottom')
for x, y in zip(x2, y2):
plt.text(x, -y - 0.05, '%.2f' % -y, ha='center', va='top')
# 设置坐标可见性
plt.xlim(-1, n)
plt.xticks(())
plt.ylim(-1.25, 1.25)
plt.yticks(())
plt.show()
等高线
import numpy as np
from matplotlib import pyplot as plt
def f(x, y):
# the height function
return (1 - x / 2 + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)
n = 256
x = np.linspace(-3, 3, n)
y = np.linspace(-3, 3, n)
X, Y = np.meshgrid(x, y)
# 添加等高线颜色,8表示划分的数量,0表示两部分
plt.contourf(X, Y, f(X, Y), 8, alpha=0.75, cmap=plt.cm.hot)
# 做等高线
cline = plt.contour(X, Y, f(X, Y), 8, colors='black')
# 添加数字描述
plt.clabel(cline, inline=True, fontsize=10)
# 设置坐标可见性
plt.xticks(())
plt.yticks(())
plt.show()
网友评论