# data curve_list = [] x = np.arange(1,100) curve_list.append((x, x)) curve_list.append((x, -x)) curve_list.append((x, x**2)) curve_list.append((x, np.log(x)))
# sub window nums n = len(curve_list) column_num = 2 row_num = int(np.ceil(n/column_num))
# construct figure and pre split fig, axes = plt.subplots(row_num, column_num, figsize=(10,10)) fig.suptitle("curve show")
# draw sub plot on figure index = 0 for row in axes: for ax in row: x, y = curve_list[index] ax.set_title("curve_{}".format(index)) # plot 时设置 label, 颜色, 线宽度等 ax.plot(x, y, 'C{}'.format(index), label='curve{}'.format(index), linewidth=2) ax.grid(color='r', linestyle='--', linewidth=1,alpha=0.3) index += 1 # ax 显示label, 可设置显示位置 ax.legend(loc='lower right', fontsize=10)
import pandas as pd import numpy as np import matplotlib.pyplot as plt fig, axes = plt.subplots(2, 2) data = pd.Series(np.random.rand(16), index=list('abcdefghijklmnop')) data.plot.bar(ax=axes[1,1], color='b', alpha = 0.5) data.plot.barh(ax=axes[0,1], color='k', alpha=0.5)
使用add_subplot
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import numpy as np import matplotlib.pyplot as plt x = np.arange(1, 100) # first you have to make the figure fig = plt.figure() # now you have to create each subplot individually ax1 = fig.add_subplot(221) ax1.plot(x, x) ax2 = fig.add_subplot(222) ax2.plot(x, -x) ax3 = fig.add_subplot(223) ax3.plot(x, x ** 2) ax4 = fig.add_subplot(224) ax4.plot(x, np.log(x)) plt.show()
#!/usr/bin/python #coding: utf-8 import numpy as np import matplotlib.pyplot as plt
x = np.arange(1, 100) # first you have to make the figure fig = plt.figure(1) # now you have to create each subplot individually plt.subplot(221) plt.plot(x, x) plt.subplot(222) plt.plot(x, -x) plt.subplot(223) plt.plot(x, x ** 2) plt.subplot(224) plt.plot(x, np.log(x)) # or # ax1 = plt.subplot(221) # ax1.plot(x, x) plt.show()
# simple example lines = plt.plot(data) plt.legend(lines, ['line1','line2','lin3'])
# another simple example from numpy.random import randn z = randn(10) red_dot, = plt.plot(z, "ro", markersize=15) # Put a white cross over some of the data. white_cross, = plt.plot(z[:5], "w+", markeredgewidth=3, markersize=15) plt.legend([red_dot, (red_dot, white_cross)], ["Attr A", "Attr A+B"])
# practice example # 注意dataframe.plot不返回Line2D对象,如果需要曲线对象,需要使用dataframe.plot.line(), 最好还是使用原始的ax.plot()分column绘制,这样好控制一些 lines = [] legends = [] for idx, metric_type inenumerate(metrics_type_column): data = df[metric_type] x, y = data.idxmax(), data.max() line = ax.plot(data, c='C%d'%(idx)) lines.extend(line) ax.scatter(x, y, c='C%d'%(idx), alpha=0.5) legends.append("%s max=%.4f"%(metric_type.replace("bf_", ""), y)) ax.legend(lines, legends, loc='lower right')