matplotlib 如何在子图中创建两个散点图[重复]

pxq42qpu  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(119)

此问题已在此处有答案

How to plot in multiple subplots(13个回答)
6天前关闭。
我尝试生成两个散点图(并排),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]))

当我试图生成一个由两个组成的面板时,我直观地定义了一个为data 1,另一个为data 2,然后继续使用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轴上的值差异很大,所以i最终具有不同的y轴间距,有没有办法解决这个问题?

dojqjjoe

dojqjjoe1#

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()

相关问题