如何单独颜色注释和共享 y 轴?

how to individually color annotate and share y axis?

我想分享两个子图的 y-axis(标题在两个图之间 y-axis。

我写了这个:

ax1,ax2 = axs

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


ax1.set_xlabel(r'$\Delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')
ax2.set_xlabel(r'$\Delta q$')

axs.get_shared_y_axes(r'$\Delta \psi_mem$  cAMP-bound wild-type mHCN2 (mV)')

抛出了这个错误:

AttributeError: 'numpy.ndarray' object 没有属性 'get_shared_y_axes'

我还需要根据我指定的颜色 class 为图上的每个点着色,不知何故,对这些点的迭代不起作用,知道吗?

我的完整代码:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches


from scipy.stats import t



data1 = np.array([
    [22.8, 22.8],
    [19.6, 0.3],
    [0.3, 3.1],
    [8.9, -1.7],
    [13.7, 4.8],
    [14.7, -0.7],
    [1.9, -2.6],
    [-1.8, -0.03],
    [-3, -5.7],
    [-5.9, -1.5],
    [-13.4, -3.9],
    [-5.7, -21.5],
    [-6.8, -7.7],
]) 

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=(17,9))
ax1,ax2 = axs

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


ax1.set_xlabel(r'$\Delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')
ax2.set_xlabel(r'$\Delta q$')

axs.get_shared_y_axes(r'$\Delta \psi_mem$  cAMP-bound wild-type mHCN2 (mV)')

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

# Defining custom 'xlim' and 'ylim' values.
custom_xlim = (-3, 3)
custom_ylim = (-25, 25)

# Setting the values for all axes.
plt.setp(axs, xlim=custom_xlim, ylim=custom_ylim)


classes = ["K464E", "K472E", "R470E", "K464A", "M155E", "K472A", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]
class_colours = ["r", "r", "r", "r", "r", "r", "g", "g", "b", "b", "b", "b", "b"]
recs = []
for i in range(0,len(class_colours)):
    recs.append(mpatches.Rectangle((0,0),1,1,fc=class_colours[i]))
plt.legend(recs,classes,loc=1)
plt.show()

编辑:在下面你会看到我已经用我希望包含的东西生成的情节。共享 y-axis 应该是黄色箭头所在的位置。关于“单独的颜色注释,我需要根据用红色箭头突出显示的 colour_class 为图上的每个点着色。

我不太确定我是否完全理解关于颜色的问题,但是您可以设置 c= 关键字参数以将单独的颜色应用于散点(参见下面的代码)。

关于共享 y-axis,可能有几个解决方案:

第一种解决方案:可以在创建figure/axes时使用关键字参数sharey=True:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches


from scipy.stats import t



data1 = np.array([
    [22.8, 22.8],
    [19.6, 0.3],
    [0.3, 3.1],
    [8.9, -1.7],
    [13.7, 4.8],
    [14.7, -0.7],
    [1.9, -2.6],
    [-1.8, -0.03],
    [-3, -5.7],
    [-5.9, -1.5],
    [-13.4, -3.9],
    [-5.7, -21.5],
    [-6.8, -7.7],
]) 

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"]
classes = ["K464E", "K472E", "R470E", "K464A", "M155E", "K472A", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]
class_colours = ["r", "r", "r", "r", "r", "r", "g", "g", "b", "b", "b", "b", "b"]

fig, axs = plt.subplots(1,2, figsize=(17,9), sharey=True)
ax1,ax2 = axs

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


ax1.set_xlabel(r'$\Delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')
ax2.set_xlabel(r'$\Delta q$')
ax1.set_ylabel(r'$\Delta \psi_mem$  cAMP-bound wild-type mHCN2 (mV)')

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

# Defining custom 'xlim' and 'ylim' values.
custom_xlim = (-3, 3)
custom_ylim = (-25, 25)

# Setting the values for all axes.
plt.setp(axs, xlim=custom_xlim, ylim=custom_ylim)

recs = []
for i in range(0,len(class_colours)):
    recs.append(mpatches.Rectangle((0,0),1,1,fc=class_colours[i]))
plt.legend(recs,classes,loc=1)
plt.show()

第二种解决方案:不使用sharey=True,你只需将第一个子图的y-axis向右移动:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches


from scipy.stats import t



data1 = np.array([
    [22.8, 22.8],
    [19.6, 0.3],
    [0.3, 3.1],
    [8.9, -1.7],
    [13.7, 4.8],
    [14.7, -0.7],
    [1.9, -2.6],
    [-1.8, -0.03],
    [-3, -5.7],
    [-5.9, -1.5],
    [-13.4, -3.9],
    [-5.7, -21.5],
    [-6.8, -7.7],
]) 

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"]
classes = ["K464E", "K472E", "R470E", "K464A", "M155E", "K472A", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]
class_colours = ["r", "r", "r", "r", "r", "r", "g", "g", "b", "b", "b", "b", "b"]

fig, axs = plt.subplots(1,2, figsize=(17,9))
ax1,ax2 = axs

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


ax1.set_xlabel(r'$\Delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')
ax1.yaxis.tick_right()
ax2.set_xlabel(r'$\Delta q$')
ax2.set_ylabel(r'$\Delta \psi_mem$  cAMP-bound wild-type mHCN2 (mV)')

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

# Defining custom 'xlim' and 'ylim' values.
custom_xlim = (-3, 3)
custom_ylim = (-25, 25)

# Setting the values for all axes.
plt.setp(axs, xlim=custom_xlim, ylim=custom_ylim)

recs = []
for i in range(0,len(class_colours)):
    recs.append(mpatches.Rectangle((0,0),1,1,fc=class_colours[i]))
plt.legend(recs,classes,loc=1)
plt.show()