使用图例
后面会专门出一期图例的推文,这里先暂时了解一下
fig, ax = plt.subplots(figsize=(3.5,2.5)) x = np.linspace(1, 10) ax.plot(x , x+2, label='$y=x+2$') ax.plot(x, x*x, label='$y=x^2$') ax.plot(x, 4*x*x + 3*x + 1, label='$y=4x^2+3x+1$') ax.legend()
水平线和垂直线
fig, (ax1,ax2) = plt.subplots(ncols=2,figsize=(3.5, 2.5)) ax1.axvline(x=5, ymax=0.5) # x=5的竖直线 ax2.axhline(y=5, xmin=0.5) # y=5的水平线 # 可以通过xmax/xmin来设置线的范围,值的范围为【0, 1】
散点图
fig, ax = plt.subplots(figsize=(3.5,2.5)) x = np.linspace(-np.pi, np.pi) ax.scatter( x, np.sin(x), color='b', marker='o', alpha=0.4, facecolor='b', edgecolor='g', )
柱状图
x = np.array([1, 2, 3, 4]) y = np.array([1, 3, 5, 7]) fig, [ax1, ax2] = plt.subplots(1, 2, figsize=(3.5,2.5)) ax1.bar( x, y, color='b', alpha=0.4, facecolor='b', edgecolor='g', ) ax1.set_title('竖着的柱状图') ax2.barh( x, y, color='b', alpha=0.4, facecolor='b', edgecolor='g', ) ax2.set_title('横着的柱状图') # 优化布局 fig.tight_layout()
误差线
errorbar
import random random.seed(0) x = np.array([1, 2, 3, 4]) y = np.array([1, 3, 5, 7]) fig, ax = plt.subplots(figsize=(3.5,2.5), tight_layout=True) ax.errorbar( x, y, yerr=[random.random() for i in x], xerr=[random.random() for i in x], capsize=4, elinewidth=2, ecolor='b', color='r', )
饼图
fig, ax = plt.subplots(figsize=(3.5,2.5), tight_layout=True) x = [1, 3, 5, 7, 9] ax.pie( x, labels=['a','b','c','d','e'], colors=[ 'b', 'r', 'g', 'y', '#f5a6c740' ], )