Skip to content

Latest commit

 

History

History
151 lines (119 loc) · 6.25 KB

draw-figs.md

File metadata and controls

151 lines (119 loc) · 6.25 KB

1. 绘图常识

  • 图类型:饼图、柱状图、直方图、箱线图、旭日图分组柱状图
  • Figure -> Axes -> Axis (label, ticks, scale, format) + Legend + Title

a. 颜色

  • 颜色合集:CSDN贴知乎贴,或者用PowerToys工具提取十六进制的RGB颜色
  • 论文常用配色:
    • ['m', 'c', 'y', 'chocolate', 'g', 'b', 'r']
    • ['crimson', 'deepskyblue', 'cornflowerblue']

b. 图例

  • 论文常用图例:['s', '^', 'v', '<', '>', '*', 'p']
  • 图例合集

2. 奇技淫巧

  • 在PPT中绘图插入不常见符号方法:

    • MathType中输入tex代码,直接拷贝
    • Word中插入公式(LaTeX模式),然后转换,参考
    • Word中选中全部Unicode字符,按Alt+X,参考
  • 设置字体、字号、线宽

plt.rc('font', family='Times NR MT Pro') # 字体全局修改
# linewidth, linestyle, markersize -- for plot
# fontsize --> for ticks
# prop = {'family': '', 'size': 20} --> for label & legend
plt.xticks() # 当没有输入参数时,以元组形式返回当前xticks和labels
from collections import OrderedDict
import matplotlib.pyplot as plt

handles, labels = plt.gca().get_legend_handles_labels()
by_label = OrderedDict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys())
import matplotlib.patches as mpatches
patch = mpatches.Patch(color='r', label='red')
plt.legend(handles=[patch])
# framealpha 控制透明度, frameon 控制边框
# handleheight/handlelength 分别控制宽和长
plt.legend(handles=patch_list, framealpha=0.5, handleheight=0.2, handlelength=1.0, frameon=True)
  • bbox_to_anchor控制图例位置,ncols控制图例列数

  • Ubuntu没有Times New Roman字体,(非管理员)解决方案:

  • 自定义Scale

    • 如果缩放不涉及infinite,则可以只定义forward_funcinverse_func函数实现:
      • plt.xscale('function', functions=(forward_func, inverse_func))
    • 如果缩放涉及到数据无效,则需要自己实现class CustomScale(mscale.ScaleBase),DET的例子中实现了$\Phi^{-1}\left(\cdot\right)$缩放,注意:将x和y轴限制到[0.08, 50],效果最好
  • 设置坐标轴FormatterLocator的方法及概念:

ax.xaxis.set_major_formatter(xmajor_formatter) # 主坐标轴格式
ax.xaxis.set_minor_formatter(xminor_formatter) # 次坐标轴格式
ax.yaxis.set_major_formatter(ymajor_formatter)
ax.yaxis.set_minor_formatter(yminor_formatter)

ax.xaxis.set_major_locator(xmajor_locator) # 主坐标轴刻度
ax.xaxis.set_minor_locator(xminor_locator) # 次坐标轴刻度
ax.yaxis.set_major_locator(ymajor_locator)
ax.yaxis.set_minor_locator(yminor_locator)
  • plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))必须与ScalarFormatter(useOffset=False, useMathText=True, useLocale=True)搭配使用,参考
  • 控制图片大小:_, axe = plt.subplots(figsize=(5.5, 4.8))
  • 隐藏某个轴(e.g. y轴):
axe.get_yaxis().set_visible(place_ylabelticks)  # 刻度、ylabel都被去除
axe.yaxis.set_major_formatter(NullFormatter())  # 保留刻度

3. 高级绘图示例

a. DET曲线绘制(GaussianScale)

b. 频谱图绘制(仿Au配色)

def plot_spectrogram(write_path_list, spec, **kwargs):
    nfeat, nframe = spec.shape
    max_freq = 8000

    plt.figure(figsize=(10, 3))             # figsize for `8s` audio, finetune
    im = plt.imshow(spec, cmap='magma')     # gnuplot, plasma, magma, inferno
    cbar = plt.colorbar(im, ticks=np.arange(40, 130, 20))
    cbar.ax.set_yticklabels([f'{p} dB' for p in np.arange(40, 130, 20)], fontsize=13)
    plt.gca().invert_yaxis()
    xlabels = np.arange(0, nframe, 100)/100.0
    ylabels = np.arange(0, max_freq+1, 2000)
    plt.xticks(np.arange(0, nframe, 100), labels=xlabels, fontsize=13)
    plt.yticks(np.arange(0, max_freq+1, 2000)/max_freq*nfeat, labels=ylabels, fontsize=13)
    plt.xlabel('Time (s)', {'size': 15})
    plt.ylabel('Frequency (Hz)', {'size': 15})
    plt.tight_layout()
    if not isinstance(write_path_list, (list, tuple)):
        write_path_list = [write_path_list]
    [plt.savefig(path) for path in write_path_list]
    plt.close()

if __name__ == "__main__":
    import torch, torchaudio
    spec_extractor = torchaudio.transforms.Spectrogram(
        n_fft=512, win_length=400, hop_length=160,
        window_fn=torch.hamming_window
    )
    spec2db = torchaudio.transforms.AmplitudeToDB(top_db=80)

    audio_path = 'dataset/vox1_test_wav/id10305/3QrLepYlH6o/00001.wav'
    audio = torchaudio.load(audio_path, normalize=False)[0].float()
    spec = spec_extractor(audio.squeeze(0))
    spec = spec2db(spec)
    plot_spectrogram('au-spec.png', spec)

c. 多Y轴绘制