如何单独着色注释并共享y轴?

发布于 2025-01-23 19:35:44 字数 2495 浏览 4 评论 0 原文

我想分享两个子图的y轴(与两个图之间的y轴标题。

我写了这篇文章:

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'对象没有属性'get_shared_y_axes'

我会还需要根据我指定的颜色类上的每个点上点,以某种方式上的迭代不起作用,

我的完整代码:

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

在下面:您看到我已经生成了我希望的事物。包括

。 ://i.sstatic.net/pwxii.png“ rel =” nofollow noreferrer“>

i would like to share the y-axis of two subplots (with the y-axis title in between the two plots.

i wrote this:

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

which threw this error:

AttributeError: 'numpy.ndarray' object has no attribute 'get_shared_y_axes'

i would also need to color each point on the plot according to the color class i specified, somehow the iteration over the points is not working, any idea?

my full code:

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

edit: below you see the plot that i already generated with the things i wish to include. the shared y-axis should be where the yellow arrow is. and about the "individual color annotation, i need to color each point on the plot according to the the colour_class highlighted with the red arrow.

enter image description here

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

which threw this error:

AttributeError: 'numpy.ndarray' object has no attribute 'get_shared_y_axes'

i would also need to color each point on the plot according to the color class i specified, somehow the iteration over the points is not working, any idea?

my full code:


edit: below you see the plot that i already generated with the things i wish to include. the shared y-axis should be where the yellow arrow is. and about the "individual color annotation, i need to color each point on the plot according to the the colour_class highlighted with the red arrow.

enter image description here

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

edit: below you see the plot that i already generated with the things i wish to include. the shared y-axis should be where the yellow arrow is. and about the "individual color annotation, i need to color each point on the plot according to the the colour_class highlighted with the red arrow.

enter image description here

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

which threw this error:

AttributeError: 'numpy.ndarray' object has no attribute 'get_shared_y_axes'

i would also need to color each point on the plot according to the color class i specified, somehow the iteration over the points is not working, any idea?

my full code:


edit: below you see the plot that i already generated with the things i wish to include. the shared y-axis should be where the yellow arrow is. and about the "individual color annotation, i need to color each point on the plot according to the the colour_class highlighted with the red arrow.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来是傀儡 2025-01-30 19:35:44

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

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

第一个解决方案:您可以使用关键字参数 sharey = true = 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

第二个解决方案:而不是使用 sharey = true ,只需向右移动第一个子图的y轴:

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

) 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轴:


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

) 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轴:


I'm not really sure if I fully understood the question about colors, however you can set the c= keyword argument to apply individual colors to scatter points (see code below).

Regarding the shared y-axis, there might be a couple of solutions:

First solution: you can use the keyword argument sharey=True when creating the figure/axes:

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

enter image description here

Second solution: instead of using sharey=True, you just move to the right the y-axis of the first subplot:

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

enter image description here

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

enter image description here

Second solution: instead of using sharey=True, you just move to the right the y-axis of the first subplot:


enter image description here

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

enter image description here

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

enter image description here

Second solution: instead of using sharey=True, you just move to the right the y-axis of the first subplot:


enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文