查看原文
其他

Matplotlib绘制的27个常用图(附对应代码实现)

生信宝典 2022-03-29

Editor's Note

Python和R,你值得拥有。

The following article is from Python与算法社区 Author zglg

0 共用模块

模块名称:example_utils.py,里面包括三个函数,各自功能如下:

import matplotlib.pyplot as plt

# 创建画图fig和axes
def setup_axes():
    fig, axes = plt.subplots(ncols=3, figsize=(6.5,3))
    for ax in fig.axes:
        ax.set(xticks=[], yticks=[])
    fig.subplots_adjust(wspace=0, left=0, right=0.93)
    return fig, axes
# 图片标题
def title(fig, text, y=0.9):
    fig.suptitle(text, size=14, y=y, weight='semibold', x=0.98, ha='right',
                 bbox=dict(boxstyle='round', fc='floralwhite', ec='#8B7E66',
                           lw=2))
# 为数据添加文本注释
def label(ax, text, y=0):
    ax.annotate(text, xy=(0.50.00), xycoords='axes fraction', ha='center',
                style='italic',
                bbox=dict(boxstyle='round', facecolor='floralwhite',
                          ec='#8B7E66'))

1 基本绘图


R语言 - 线图绘制

对应代码:

import numpy as np
import matplotlib.pyplot as plt

import example_utils

x = np.linspace(010100)

fig, axes = example_utils.setup_axes()
for ax in axes:
    ax.margins(y=0.10)

# 子图1 默认plot多条线,颜色系统分配
for i in range(16):
    axes[0].plot(x, i * x)

# 子图2 展示线的不同linestyle
for i, ls in enumerate(['-''--'':''-.']):
    axes[1].plot(x, np.cos(x) + i, linestyle=ls)

# 子图3 展示线的不同linestyle和marker
for i, (ls, mk) in enumerate(zip(['''-'':'], ['o''^''s'])):
    axes[2].plot(x, np.cos(x) + i * x, linestyle=ls, marker=mk, markevery=10)

# 设置标题
# example_utils.title(fig, '"ax.plot(x, y, ...)": Lines and/or markers', y=0.95)
# 保存图片
fig.savefig('plot_example.png', facecolor='none')
# 展示图片
plt.show()

2 散点图

R语言 - 箱线图(小提琴图、抖动图、区域散点图)

对应代码:

"""
散点图的基本用法
"""

import numpy as np
import matplotlib.pyplot as plt

import example_utils

# 随机生成数据
np.random.seed(1874)
x, y, z = np.random.normal(01, (3100))
t = np.arctan2(y, x)
size = 50 * np.cos(2 * t)**2 + 10

fig, axes = example_utils.setup_axes()

# 子图1
axes[0].scatter(x, y, marker='o',  color='darkblue', facecolor='white', s=80)
example_utils.label(axes[0], 'scatter(x, y)')

# 子图2
axes[1].scatter(x, y, marker='s', color='darkblue', s=size)
example_utils.label(axes[1], 'scatter(x, y, s)')

# 子图3
axes[2].scatter(x, y, s=size, c=z,  cmap='gist_ncar')
example_utils.label(axes[2], 'scatter(x, y, s, c)')

# example_utils.title(fig, '"ax.scatter(...)": Colored/scaled markers',
#                     y=0.95)
fig.savefig('scatter_example.png', facecolor='none')

plt.show()

3 柱状图

R语言 - 柱状图

对应代码:

import numpy as np
import matplotlib.pyplot as plt

import example_utils


def main():
    fig, axes = example_utils.setup_axes()

    basic_bar(axes[0])
    tornado(axes[1])
    general(axes[2])

    # example_utils.title(fig, '"ax.bar(...)": Plot rectangles')
    fig.savefig('bar_example.png', facecolor='none')
    plt.show()

# 子图1
def basic_bar(ax):
    y = [1345.532]
    err = [0.212.5110.5]
    x = np.arange(len(y))
    ax.bar(x, y, yerr=err, color='lightblue', ecolor='black')
    ax.margins(0.05)
    ax.set_ylim(bottom=0)
    example_utils.label(ax, 'bar(x, y, yerr=e)')

# 子图2
def tornado(ax):
    y = np.arange(8)
    x1 = y + np.random.random(8) + 1
    x2 = y + 3 * np.random.random(8) + 1
    ax.barh(y, x1, color='lightblue')
    ax.barh(y, -x2, color='salmon')
    ax.margins(0.15)
    example_utils.label(ax, 'barh(x, y)')

# 子图3
def general(ax):
    num = 10
    left = np.random.randint(010, num)
    bottom = np.random.randint(010, num)
    width = np.random.random(num) + 0.5
    height = np.random.random(num) + 0.5
    ax.bar(left, height, width, bottom, color='salmon')
    ax.margins(0.15)
    example_utils.label(ax, 'bar(l, h, w, b)')


main()

4 填充画图

对应代码:

"""
fill函数的各种用法
"""

import numpy as np
import matplotlib.pyplot as plt

import example_utils


# -- 产生数据 ----------------------


def stackplot_data():
    x = np.linspace(010100)
    y = np.random.normal(01, (5100))
    y = y.cumsum(axis=1)
    y -= y.min(axis=0, keepdims=True)
    return x, y


def sin_data():
    x = np.linspace(010100)
    y = np.sin(x)
    y2 = np.cos(x)
    return x, y, y2


def fill_data():
    t = np.linspace(02*np.pi, 100)
    r = np.random.normal(01100).cumsum()
    r -= r.min()
    return r * np.cos(t), r * np.sin(t)


def fill_example(ax):
    # fill一个多边形区域
    x, y = fill_data()
    ax.fill(x, y, color='lightblue')
    ax.margins(0.1)
    example_utils.label(ax, 'fill')


def fill_between_example(ax):
    # 两条线间填充
    x, y1, y2 = sin_data()

    # fill_between的最常用法1
    err = np.random.rand(x.size)**2 + 0.1
    y = 0.7 * x + 2
    ax.fill_between(x, y + err, y - err, color='orange')

    # 最常用法2:两条曲线相交区域对应不同填充色
    ax.fill_between(x, y1, y2, where=y1 > y2, color='lightblue')
    ax.fill_between(x, y1, y2, where=y1 < y2, color='forestgreen')

    # 最常用法3
    ax.fill_betweenx(x, -y1, where=y1 > 0, color='red', alpha=0.5)
    ax.fill_betweenx(x, -y1, where=y1 < 0, color='blue', alpha=0.5)

    ax.margins(0.15)
    example_utils.label(ax, 'fill_between/x')


def stackplot_example(ax):
    # Stackplot就是多次调用 ax.fill_between
    x, y = stackplot_data()
    ax.stackplot(x, y.cumsum(axis=0), alpha=0.5)
    example_utils.label(ax, 'stackplot')


def main():
    fig, axes = example_utils.setup_axes()

    fill_example(axes[0])
    fill_between_example(axes[1])
    stackplot_example(axes[2])

    # example_utils.title(fig, 'fill/fill_between/stackplot: Filled polygons',
    #                     y=0.95)
    fig.savefig('fill_example.png', facecolor='none')
    plt.show()


main()

5 imshow

一个震撼的交互型3D可视化R包 - 可直接转ggplot2图为3D

对应代码:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.cbook import get_sample_data
from mpl_toolkits import axes_grid1

import example_utils


def main():
    fig, axes = setup_axes()
    plot(axes, *load_data())
    # example_utils.title(fig, '"ax.imshow(data, ...)": Colormapped or RGB arrays')
    fig.savefig('imshow_example.png', facecolor='none')
    plt.show()


def plot(axes, img_data, scalar_data, ny):

    # 默认线性插值
    axes[0].imshow(scalar_data, cmap='gist_earth', extent=[0, ny, ny, 0])

    # 最近邻插值
    axes[1].imshow(scalar_data, cmap='gist_earth', interpolation='nearest',
                   extent=[0, ny, ny, 0])

    # 展示RGB/RGBA数据
    axes[2].imshow(img_data)


def load_data():
    img_data = plt.imread(get_sample_data('5.png'))
    ny, nx, nbands = img_data.shape
    scalar_data = np.load(get_sample_data('bivariate_normal.npy'))
    return img_data, scalar_data, ny


def setup_axes():
    fig = plt.figure(figsize=(63))
    axes = axes_grid1.ImageGrid(fig, [00.931], (13), axes_pad=0)

    for ax in axes:
        ax.set(xticks=[], yticks=[])
    return fig, axes


main()

6 pcolor

还在用PCA降维?快学学大牛最爱的t-SNE算法吧, 附Python/R代码

对应代码:

"""
pcolor/pcolormesh的基本用法
记住一点:假如数据在矩形区域内建议使用imshow,这样速度更快。此例子展示imshow不能使用的场景

"""

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.cbook import get_sample_data

import example_utils

# 拿到数据 ...
z = np.load(get_sample_data('./bivariate_normal.npy'))
ny, nx = z.shape
y, x = np.mgrid[:ny, :nx]
y = (y - y.mean()) * (x + 10)**2

mask = (z > -0.1) & (z < 0.1)
z2 = np.ma.masked_where(mask, z)

fig, axes = example_utils.setup_axes()

# pcolor 或 pcolormesh 都可,后者效率更高
axes[0].pcolor(x, y, z, cmap='gist_earth')
example_utils.label(axes[0], 'either')

# pcolor和pcolormesh的不同展示
# 使用pcolor
axes[1].pcolor(x, y, z2, cmap='gist_earth', edgecolor='black')
example_utils.label(axes[1], 'pcolor(x,y,z)')

# 使用pcolormesh
axes[2].pcolormesh(x, y, z2, cmap='gist_earth', edgecolor='black', lw=0.5,
                   antialiased=True)
example_utils.label(axes[2], 'pcolormesh(x,y,z)')

#example_utils.title(fig, 'pcolor/pcolormesh: Colormapped 2D arrays')
fig.savefig('pcolor_example.png', facecolor='none')

plt.show()

7 contour

对应代码:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.cbook import get_sample_data

import example_utils

z = np.load(get_sample_data('bivariate_normal.npy'))

fig, axes = example_utils.setup_axes()

axes[0].contour(z, cmap='gist_earth')
example_utils.label(axes[0], 'contour')

axes[1].contourf(z, cmap='gist_earth')
example_utils.label(axes[1], 'contourf')

axes[2].contourf(z, cmap='gist_earth')
cont = axes[2].contour(z, colors='black')
axes[2].clabel(cont, fontsize=6)
example_utils.label(axes[2], 'contourf + contour\n + clabel')

# example_utils.title(fig, '"contour, contourf, clabel": Contour/label 2D data',
#                     y=0.96)
fig.savefig('contour_example.png', facecolor='none')

plt.show()

8 向量场

对应代码:

import matplotlib.pyplot as plt
import numpy as np

import example_utils

# Generate data
n = 256
x = np.linspace(-33, n)
y = np.linspace(-33, n)
xi, yi = np.meshgrid(x, y)
z = (1 - xi / 2 + xi**5 + yi**3) * np.exp(-xi**2 - yi**2)
dy, dx = np.gradient(z)
mag = np.hypot(dx, dy)

fig, axes = example_utils.setup_axes()

# 单箭头
axes[0].arrow(00-0.50.5, width=0.005, color='black')
axes[0].axis([-11-11])
example_utils.label(axes[0], 'arrow(x, y, dx, dy)')

# ax.quiver
ds = np.s_[::16, ::16]  # Downsample our array a bit...
axes[1].quiver(xi[ds], yi[ds], dx[ds], dy[ds], z[ds], cmap='gist_earth',
               width=0.01, scale=0.25, pivot='middle')
axes[1].axis('tight')
example_utils.label(axes[1], 'quiver(x, y, dx, dy)')

# ax.streamplot
# 宽度和颜色变化
lw = 2 * (mag - mag.min()) / mag.ptp() + 0.2
axes[2].streamplot(xi, yi, dx, dy, color=z, density=1.5, linewidth=lw,
                   cmap='gist_earth')
example_utils.label(axes[2], 'streamplot(x, y, dx, dy)')

# example_utils.title(fig, '"arrow/quiver/streamplot": Vector fields', y=0.96)
# fig.savefig('vector_example.png', facecolor='none')

plt.show()

9 数据分布图

R语言 - 箱线图一步法

对应代码:

"""
Matplotlib 提供许多专业的绘制统计学相关的图函数

更多统计学相关图可使用 Seaborn 库,它基于Matplotlib编写。 
"""

import numpy as np
import matplotlib.pyplot as plt

import example_utils


def main():
    colors = ['cyan''red''blue''green''purple']
    dists = generate_data()

    fig, axes = example_utils.setup_axes()
    hist(axes[0], dists, colors)
    boxplot(axes[1], dists, colors)
    violinplot(axes[2], dists, colors)

    # example_utils.title(fig, 'hist/boxplot/violinplot: Statistical plotting',
    #                     y=0.9)
    fig.savefig('statistical_example.png', facecolor='none')

    plt.show()


def generate_data():
    means = [0-12.54.3-3.6]
    sigmas = [1.2531.52]
    # 每一个分布的样本个数
    nums = [1501000100200500]

    dists = [np.random.normal(*args) for args in zip(means, sigmas, nums)]
    return dists

# 频率分布直方图
def hist(ax, dists, colors):
    ax.set_color_cycle(colors)
    for dist in dists:
        ax.hist(dist, bins=20, density=True, edgecolor='none', alpha=0.5)

    ax.margins(y=0.05)
    ax.set_ylim(bottom=0)

    example_utils.label(ax, 'ax.hist(dists)')

# 箱型图
def boxplot(ax, dists, colors):
    result = ax.boxplot(dists, patch_artist=True, notch=True, vert=False)

    for box, color in zip(result['boxes'], colors):
        box.set(facecolor=color, alpha=0.5)
    for item in ['whiskers''caps''medians']:
        plt.setp(result[item], color='gray', linewidth=1.5)
    plt.setp(result['fliers'], markeredgecolor='gray', markeredgewidth=1.5)
    plt.setp(result['medians'], color='black')

    ax.margins(0.05)
    ax.set(yticks=[], ylim=[06])

    example_utils.label(ax, 'ax.boxplot(dists)')

#小提琴图
def violinplot(ax, dists, colors):
    result = ax.violinplot(dists, vert=False, showmedians=True)
    for body, color in zip(result['bodies'], colors):
        body.set(facecolor=color, alpha=0.5)
    for item in ['cbars''cmaxes''cmins''cmedians']:
        plt.setp(result[item], edgecolor='gray', linewidth=1.5)
    plt.setp(result['cmedians'], edgecolor='black')

    ax.margins(0.05)
    ax.set(ylim=[06])

    example_utils.label(ax, 'ax.violinplot(dists)')


main()

本文参考:

https://nbviewer.jupyter.org/github/matplotlib/AnatomyOfMatplotlib/blob/master/AnatomyOfMatplotlib-Part2-Plotting_Methods_Overview.ipynb

更多:

数据分析最有用的25个 Matplotlib图


Python

R统计和作图

高颜值免费在线绘图




往期精品

画图三字经 生信视频 生信系列教程 

心得体会 TCGA数据库 Linux Python 

高通量分析 免费在线画图 测序历史 超级增强子

生信学习视频 PPT EXCEL 文章写作 ggplot2

海哥组学 可视化套路 基因组浏览器

色彩搭配 图形排版 互作网络

自学生信 2019影响因子 GSEA 单细胞 

后台回复“生信宝典福利第一波”或点击阅读原文获取教程合集




您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存