python matplotlib 绘图操作

文章目录

  • 坐标操作
    • 对数坐标轴
    • 坐标刻度及其标记
    • 横坐标值逆序显示
  • 边框操作
    • 隐藏边框
  • 图例操作
    • 图例置于边框外
  • 标题操作
    • 标题位置
  • 注释操作
    • 注释字体
  • 配色操作
    • 饼图颜色
  • 子图操作
    • 子图间距


坐标操作

参考博文
Python绘图总结(Matplotlib篇)之坐标轴及刻度

对数坐标轴

import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as sts

if __name__ == '__main__':
    a = 0.031 / 10000 + 0.0337 / 10000
    print(0.0336 * 100 / np.sqrt(a))  # 1320.95

    r = sts.lognorm.rvs(0.954, size=1000)
    c = plt.hist(r, bins=500)
    plt.show()

    # 双对数坐标下
    fig, ax = plt.subplots()
    ax.set_xscale("log")
    ax.set_yscale("log")
    ax.set_adjustable("datalim")
    ax.plot(c[1][:-1], c[0], 'o')
    ax.set_xlim(1e-1, 1e6)
    ax.set_ylim(1e-2, 1e6)
    ax.grid()
    plt.draw()
    plt.show()

    # 半对数坐标
    fig1, ax1 = plt.subplots()
    ax1.hist(r, bins=500)
    ax1.set_xscale("log")
    ax1.set_xlim(1e-1, 1e6)
    ax1.grid()
    plt.draw()
    plt.show()

效果如下
在这里插入图片描述

坐标刻度及其标记

python_matplotlib改变横坐标和纵坐标上的刻度(ticks)

font = {'family': 'arial', 'weight': 'normal', 'size': 14} 
ax1.set_xticks([1,2,3,4,5)
ax1.set_xticklabels(x,font) 
ax1.set_yticks([0,0.2,0.4,0.6,0.8,1])
ax1.set_yticklabels([0,0.2,0.4,0.6,0.8,1],font) 

横坐标值逆序显示

逆序前的代码和图

import matplotlib.pyplot as plt

plt.figure()
ax1 = plt.subplot(111)
x_list = [1, 2, 3, 4, 5]
y_list = [5, 10, 20, 40, 80]
plt.sca(ax1)
plt.title("Test 1")
plt.xlabel("X")
plt.ylabel("Y")
plot1, = plt.plot(x_list, y_list, 'bo')
plt.show()

在这里插入图片描述
逆序后的代码和图

import matplotlib.pyplot as plt

plt.figure()
ax1 = plt.subplot(111)
x_list = [1, 2, 3, 4, 5]
y_list = [5, 10, 20, 40, 80]
plt.sca(ax1)
plt.title("Test 1")
plt.xlabel("X")
plt.ylabel("Y")
plot1, = plt.plot(x_list, y_list, 'bo')
plt.gca().invert_xaxis()  # 就这句话
plt.show()

在这里插入图片描述

边框操作

隐藏边框

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

图例操作

参考博文
matplotlib命令与格式:图例legend语法及设置

图例置于边框外

参考博文

ax.legend(loc='center left', bbox_to_anchor=(0.2, 1.12),ncol=3)

标题操作

官方教程

标题位置

ax.set_title('AUC', font, x=0.5, y=1.05)

x,y可以控制标题的位置,但是记住要放在 font 后面,不然会报错.

SyntaxError: positional argument follows keyword argument

注释操作

matplotlib.axes.Axes.annotate
matplotlib命令与格式:标题(title),标注(annotate),文字说明(text)

注释字体

ax.annotate('{}'.format(height),
             xy=(rect.get_x() + rect.get_width() / 2, height),
             xytext=(0, 3),  # 3 points vertical offset
             textcoords="offset points", 
             # 字体属性
			 size=12, family='arial', 
             ha='center', va='bottom')

配色操作

用色环图帮你搞定配色

饼图颜色

#  色系 浅灰色 #d8dcd6   天蓝色 #069af3 lightblue(#7bc8f6)  blue with a hint of purple(#533cc6)   # purple red(#990147)
# dark royal blue(#02066f) royal(#0c1793)
ax.plot(a,b,linestyle='dotted',linewidth=2.5,marker='o',color='#e377c2',label='CN')
ax.plot(a,c,linestyle='dotted',linewidth=2.5,marker='v',color='#533cc6',label='RA')
ax.plot(a,d,linestyle='solid',linewidth=2.5,marker='d',color='#7d7f7c',label='PA')
ax.plot(a,e,linestyle='dashed',linewidth=2.5,marker='8',color='#01ff07',label='LP')    

子图操作

子图间距

matplotlib.pyplot.subplots_adjust

plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)

## 默认参数
left = 0.125  # the left side of the subplots of the figure
right = 0.9   # the right side of the subplots of the figure
bottom = 0.1  # the bottom of the subplots of the figure
top = 0.9     # the top of the subplots of the figure
wspace = 0.2  # the amount of width reserved for space between subplots,
              # expressed as a fraction of the average axis width
hspace = 0.2  # the amount of height reserved for space between subplots,
              # expressed as a fraction of the average axis height

你可能感兴趣的:(数据科学,Python)