matplotlib细节设置

    技术2022-07-11  114

    坐标轴

    plt.figure(figsize=(8,3)) #原始图像 plt.subplot(131) fig = plt.gca() plt.plot(x,y) #去掉轴标签和刻度 plt.subplot(132) fig = plt.gca() plt.plot(x,y) fig.axes.get_xaxis().set_visible(False) fig.axes.get_yaxis().set_visible(False) #去掉上边和右边轴线 plt.subplot(133) fig = plt.gca() plt.plot(x,y) fig.spines['top'].set_visible(False) fig.spines['right'].set_visible(False)

    x轴标签倾斜

    labels = ['andy' for i in range(10)] fig,ax = plt.subplots() plt.plot(x,y) plt.title('andy') ax.set_xticklabels(labels,rotation = 45,horizontalalignment='right')

    图例

    x = np.arange(10) plt.figure(figsize=(12,8)) #基本图例显示 plt.subplot(221) for i in range(1,4): plt.plot(x,i*x**2,label = 'Group %d'%i) plt.legend(loc='best') #图例显示在轴上 ax=plt.subplot(222) for i in range(1,4): plt.plot(x,i*x**2,label = 'Group %d'%i) ax.legend(loc='upper center',bbox_to_anchor = (0.5,1.15) ,ncol=3) #图例显示在轴里,透明 plt.subplot(223) for i in range(1,4): plt.plot(x,i*x**2,label = 'Group %d'%i,marker='o') plt.legend(loc='upper right',framealpha = 0.1) #图例显示在轴外边右侧 ax=plt.subplot(224) for i in range(1,4): plt.plot(x,i*x**2,label = 'Group %d'%i) ax.legend(loc='upper center',bbox_to_anchor = (1.15,1) ,ncol=1)

    Processed: 0.009, SQL: 9