Matplotlib 代码模版
记录一下自己用 Matplot 画图使用的一些模版代码
全局配置
1 2 3 4 5 6 7 8 9 10 |
# 字体与字号 font = {'family': 'serif', 'serif': 'Times New Roman', 'weight': 'normal', 'size': 10} plt.rc('font', **font) ## 解决某些情况下图中的字体会不正确,见:https://github.com/matplotlib/matplotlib/issues/8017 plt.rc('mathtext', default='regular') # 保存分辨率 plt.rcParams['savefig.dpi'] = 240 plt.rcParams['figure.dpi'] = 240 |
brewer2mpl 配对色
一共12种,共6组颜色(奇数颜色与偶数颜色组成深色与浅色的对比)
1 2 3 4 5 |
# bmap = brewer2mpl.get_map('Paired', 'qualitative', 12) # colors = bmap.mpl_colors colors = [(0.6509803921568628, 0.807843137254902, 0.8901960784313725), (0.12156862745098039, 0.47058823529411764, 0.7058823529411765), (0.6980392156862745, 0.8745098039215686, 0.5411764705882353), (0.2, 0.6274509803921569, 0.17254901960784313), (0.984313725490196, 0.6039215686274509, 0.6), (0.8901960784313725, 0.10196078431372549, 0.10980392156862745), (0.9921568627450981, 0.7490196078431373, 0.43529411764705883), (1.0, 0.4980392156862745, 0.0), (0.792156862745098, 0.6980392156862745, 0.8392156862745098), (0.41568627450980394, 0.23921568627450981, 0.6039215686274509), (1.0, 1.0, 0.6), (0.6941176470588235, 0.34901960784313724, 0.1568627450980392)] |
brweer2mpl 单色
1 2 3 |
# colors = brewer2mpl.get_map('Set1', 'qualitative', 9).mpl_colors colors = [(0.8941176470588236, 0.10196078431372549, 0.10980392156862745), (0.21568627450980393, 0.49411764705882355, 0.7215686274509804), (0.30196078431372547, 0.6862745098039216, 0.2901960784313726), (0.596078431372549, 0.3058823529411765, 0.6392156862745098), (1.0, 0.4980392156862745, 0.0), (1.0, 1.0, 0.2), (0.6509803921568628, 0.33725490196078434, 0.1568627450980392), (0.9686274509803922, 0.5058823529411764, 0.7490196078431373), (0.6, 0.6, 0.6)] |
1 2 3 |
# colors = brewer2mpl.get_map('Set3', 'qualitative', 12).mpl_colors colors = [(0.5529411764705883, 0.8274509803921568, 0.7803921568627451), (1.0, 1.0, 0.7019607843137254), (0.7450980392156863, 0.7294117647058823, 0.8549019607843137), (0.984313725490196, 0.5019607843137255, 0.4470588235294118), (0.5019607843137255, 0.6941176470588235, 0.8274509803921568), (0.9921568627450981, 0.7058823529411765, 0.3843137254901961),(0.7019607843137254, 0.8705882352941177, 0.4117647058823529), (0.9882352941176471, 0.803921568627451, 0.8980392156862745), (0.8509803921568627, 0.8509803921568627, 0.8509803921568627), (0.7372549019607844, 0.5019607843137255, 0.7411764705882353), (0.8, 0.9215686274509803, 0.7725490196078432), (1.0, 0.9294117647058824, 0.43529411764705883)] |
折线样式
1 2 3 |
lines = ["-^", "-D", "-s", "-p", "-x", "-v", "-+", "-h", "-o", "-<", "->", "-p", "-x", "-v", "-"] dash_lines = [":", "--", "-."] |
模版代码
单图模版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
plt.title("Title", size=18) # 绘制带 Stdev 区间的图 plt.plot(X, mean, "-D", label="Line", color="r") plt.fill_between(X, mean - stdev, mean + stdev, color="r", alpha=0.5) # X、Y 坐标轴标题 plt.xlabel("Epoches", size=18) plt.ylabel("AUROC", size=18) # X、Y 坐标轴刻度 plt.xticks(X, X_labels, fontsize=12, rotation=0) plt.yticks(Y, Y_labels, fontsize=12) plt.legend(fontsize=12, loc="lower left") plt.tight_layout() plt.grid() plt.show() |
多图模版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# 设置子图情况 行、列、图像大小、限制布局、共享 X 轴、共享 Y 轴 fig, axs = plt.subplots(1, 2, figsize=(7, 3), constrained_layout=True, sharex=True, sharey=True) # 绘制 axs[0] axs[0].plot(X, mean, "-D", label="Line", color="r") axs[0].set_title(title, fontsize=12) # 标题 axs[0].set_xticks(X, X_ticks) # X轴刻度 axs[0].set_yticks(Y, Y_ticks) # Y轴刻度 axs[0].tick_params(axis="both", labelsize=12) # X、Y轴参数 axs[0].grid() # 网格 axs[0].legend(loc="lower right", fontsize=12) # 图注 # …… 绘制 axs[1] # 设置图标超级标题与超级X、Y轴标题 fig.suptitle("Influence of hyper-parameter $s$", size=18) fig.supxlabel("The value of $s$", size=18) fig.supylabel("G-Mean", size=18) plt.show() |

原文链接:Matplotlib 代码模版
WNJXYKの博客 版权所有,转载请注明出处。
评论功能已经关闭!