如何创建两个散点图的面板?

how to create a panel of two scatter plots?

我尝试生成两个散点图(并排),matplotlib 中的代码非常简单明了。然而,我的数据集有点复杂,这意味着对于每个 x 值,都有 2-3 个对应的 y 值,所以我为此创建了一个数组。

这里是生成一个图的代码:

import matplotlib.pyplot as plt
import numpy as np


plt.close('all')


data = np.array([
    [-2, 22.8],
    [-2, 0.3],
    [-2, 3.1],
    [-1, -1.7],
    [-1, 4.8],
    [-1, -0.7],
    [ 0, -2.6],
    [ 0, -0.03],
    [ 1, -5.7],
    [ 1, -1.5],
    [ 1, -3.9],
    [ 2, -21.5],
    [ 2, -7.7],
]) 

custom_annotations = ["K464E", "K472E", "R470E", "K464A", "M155E", "K472A", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]

plt.scatter(data[:,0], data[:,1], marker='o', c=data[:,1], edgecolors='black', linewidths=1, alpha=0.75)

# plt.colorbar(orientation='horizontal')
plt.xlabel(r'$\Delta q$')
plt.ylabel(r'$\delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')

plt.axvline(0, c=(.5, .5, .5), ls= '--')
plt.axhline(0, c=(.5, .5, .5), ls= '--')



for i, txt in enumerate(custom_annotations):
    plt.annotate(txt, (data[i,0], data[i,1]))

当我尝试生成两个面板时,我直观地将一个定义为 data1,另一个定义为 data2,然后继续使用 matplotlib 代码,方法如下:

import matplotlib.pyplot as plt
import numpy as np



data1 = np.array([
    [-2, 22.8],
    [-2, 19.6],
    [-2, 0.3],
    [-1, 8.9],
    [-1, 13.7],
    [-1, 14.7,],
    [ 0, 1.9],
    [ 0, -1.8],
    [ 1, -3],
    [ 1, -5.9],
    [ 1, -13.4],
    [ 2, -5.7],
    [ 2, -6.8],
]) 

data2 =  np.array([
    [-2, 22.8],
    [-2, 0.3],
    [-2, 3.1],
    [-1, -1.7],
    [-1, 4.8],
    [-1, -0.7],
    [ 0, -2.6],
    [ 0, -0.03],
    [ 1, -5.7],
    [ 1, -1.5],
    [ 1, -3.9],
    [ 2, -21.5],
    [ 2, -7.7],
]) 


custom_annotations = ["K464E", "K472E", "R470E", "K464A", "M155E", "K472A", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]


ax1.scatter(data1[:,0], data1[:,1])
ax2.scatter(data2[:,0], data2[:,1])

ax1.set_title('Experimental Apo mHCN2 Channel')
ax1.set_xlabel(r'$\Delta q$')
ax1.set_ylabel(r'$\delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')

ax2.set_title('Electrostatic Potential Calculations - Holo mHCN2 Channel')
ax2.set_xlabel(r'$\Delta q$')
ax2.set_ylabel(r'$\delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')

plt.axvline(0, c=(.5, .5, .5), ls= '--')
plt.axhline(0, c=(.5, .5, .5), ls= '--')



for i, txt in enumerate(custom_annotations):
    plt.annotate(txt, (data1[i,0], data1[i,1]))
    plt.annotate(txt, (data2[i,0], data2[i,1]))


plt.show()

这生成了一个空的情节,所以应该有什么致命的错误。

此外,由于两个数据集之间 y 轴上的值差异很大,我最终得到了不同的 y 轴间距,有没有办法解决这个问题?

import matplotlib.pyplot as plt
import numpy as np

data1 = np.array([
    [-2, 22.8],
    [-2, 19.6],
    [-2, 0.3],
    [-1, 8.9],
    [-1, 13.7],
    [-1, 14.7,],
    [ 0, 1.9],
    [ 0, -1.8],
    [ 1, -3],
    [ 1, -5.9],
    [ 1, -13.4],
    [ 2, -5.7],
    [ 2, -6.8],
]) 

data2 =  np.array([
    [-2, 22.8],
    [-2, 0.3],
    [-2, 3.1],
    [-1, -1.7],
    [-1, 4.8],
    [-1, -0.7],
    [ 0, -2.6],
    [ 0, -0.03],
    [ 1, -5.7],
    [ 1, -1.5],
    [ 1, -3.9],
    [ 2, -21.5],
    [ 2, -7.7],
]) 

custom_annotations = ["K464E", "K472E", "R470E", "K464A", "M155E", "K472A", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]

fig, axs = plt.subplots(1,2, figsize=(12,9), sharey='row')
ax1,ax2 = axs

ax1.scatter(data1[:,0], data1[:,1])
ax2.scatter(data2[:,0], data2[:,1])

ax1.set_title('Experimental Apo mHCN2 Channel')
ax1.set_xlabel(r'$\Delta q$')
ax1.set_ylabel(r'$\delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')

ax2.set_title('Electrostatic Potential Calculations - Holo mHCN2 Channel')
ax2.set_xlabel(r'$\Delta q$')

for ax in axs:
    ax.axvline(0, c=(.5, .5, .5), ls= '--')
    ax.axhline(0, c=(.5, .5, .5), ls= '--')

for i, txt in enumerate(custom_annotations):
    ax1.annotate(txt, (data1[i,0], data1[i,1]))
    ax2.annotate(txt, (data2[i,0], data2[i,1]))
    
plt.show()