matplotlib
实例一:
import matplotlib.pyplot as plt import numpy as np x = np.linspace(-3, 3, 50) y1 = x ** 2 y2 = 2 * x + 1 plt.figure(num=1, figsize=(8, 5)) plt.xlim((-1, 2)) plt.ylim((-2, 3)) plt.xlabel('I am x') plt.ylabel('I am y') new_ticks = np.linspace(-1, 2, 5) print(new_ticks) plt.xticks(new_ticks) plt.yticks([-1, 2], ['really good', 'good']) 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)) l1, = plt.plot(x, y1, label='up') l2, = plt.plot(x, y2, color='red', linewidth=1.0, linestyle='--', label='down') plt.legend(handles=[l1, l2], labels=['aaa', 'bbb'], loc='best') x0 = 0 y0 = 2 * x0 + 1 plt.scatter(x0, y0, s=50, color='b') plt.plot([x0, x0], [0, y0], '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(-1, 1, r'$This\ is\ the\ some\ text.$', fontdict={'size': 12, 'color': 'r'}) for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(12) label.set_bbox(dict(facecolor='white', edgecolor='none', alpha=0.7)) plt.show()
实例二:
import matplotlib.pyplot as plt import numpy as np n = 1024 X = np.random.normal(0, 1, n) Y = np.random.normal(0, 1, n) T = np.arctan2(Y, X) plt.scatter(X, Y, s=10, c=T, alpha=0.5) plt.xlim((-1.5, 1.5)) plt.ylim((-1.5, 1.5)) plt.xticks(()) plt.yticks(()) plt.show()
实例三:
import matplotlib.pyplot as plt import numpy as np 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') for x, y in zip(X, Y1): plt.text(x + 0.1, y + 0.05, '%.2f' % y, ha='center', va='bottom') for x, y in zip(X, Y2): plt.text(x + 0.1, -y - 0.05, '%.2f' % -y, ha='center', va='top') plt.xlim(-0.5, n) plt.xticks(()) plt.ylim(-1.25, 1.25) plt.yticks(()) plt.show()
实例四:
import matplotlib.pyplot as plt import numpy as np 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) plt.contourf(X, Y, f(X, Y), 8, alpha=0.5, cmap=plt.cm.hot) C = plt.contour(X, Y, f(X, Y), 8, colors='black', linewidth=0.5) plt.clabel(C, inline=True, fontsize=10) plt.xticks(()) plt.yticks(()) plt.show()