如何在单个散点图中添加注释的颜色

发布于 2025-01-24 01:56:28 字数 1910 浏览 2 评论 0原文

我需要如下图(来自两个图的图片中的图片中的一个图)上涂上注释。

因此,我想在下面旋转此图像

”旧情节“

,就图上的每个点的颜色注释而言,也添加了一个传奇,就像下面的图像

i.sstatic.net/3xfka.png IS:

 import matplotlib.pyplot as plt
    import numpy as np
    import matplotlib.patches as mpatches
    from matplotlib.pyplot import figure
    
    figure(figsize=(10, 8), dpi=120)
    
    
    from scipy.stats import t
    
    plt.close('all')
    
    
    data = 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],
    ]) 
    
    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  V_{0.5}$  Apo wild-type mHCN2 (mV)')
    plt.ylabel(r'$\Delta \psi$  cAMP-bound wild-type mHCN2 (mV)')
    
    plt.axvline(0, c=(.5, .5, .5), ls= '--')
    plt.axhline(0, c=(.5, .5, .5), ls= '--')
    
    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"]
    plt.annotate(txt, (data[i,0], data[i,1]))
    plt.legend(recs,classes,loc=(1.04,0))
    
    plt.show()

for循环中的某些内容是以不正确的方式丢失或写入的,关于点颜色注释和传说,应与帖子中的图像相同,应将其放置在图外。

i need to color annotate as in the image below (from a panel of two plots) in a single plot.

so, i would want to turn this image below

old plot

to this, in terms of the color annotation of each point on the plot, also adding a legend just as the image below

wished plot

my full code is:

 import matplotlib.pyplot as plt
    import numpy as np
    import matplotlib.patches as mpatches
    from matplotlib.pyplot import figure
    
    figure(figsize=(10, 8), dpi=120)
    
    
    from scipy.stats import t
    
    plt.close('all')
    
    
    data = 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],
    ]) 
    
    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  V_{0.5}$  Apo wild-type mHCN2 (mV)')
    plt.ylabel(r'$\Delta \psi$  cAMP-bound wild-type mHCN2 (mV)')
    
    plt.axvline(0, c=(.5, .5, .5), ls= '--')
    plt.axhline(0, c=(.5, .5, .5), ls= '--')
    
    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"]
    plt.annotate(txt, (data[i,0], data[i,1]))
    plt.legend(recs,classes,loc=(1.04,0))
    
    plt.show()

something in the for loop is missing or written in an incorrect way, the wished plot should be the same as the image in the post, with respect to points color annotation and the legend, which should be placed outside the plot.

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

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

发布评论

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

评论(1

壹場煙雨 2025-01-31 01:56:28

首先,要产生单独的注释,您的问题是没有循环。您需要为i放置,在 plt.annotate 之前枚举(类)中的txt:,

以确切地产生您想要的东西,但是,您需要执行stacter命令每个点,并将标签与每个点相关联,如下所示:

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

figure(figsize=(10, 8), dpi=120)

from scipy.stats import t

plt.close('all')


data = 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],
]) 

custom_annotations = ["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"]

for i, point in enumerate(data): 
    plt.scatter(point[0], point[1], marker='o', label=custom_annotations[i], c=class_colours[i], edgecolors='black', linewidths=1, alpha=0.75)
    plt.annotate(custom_annotations[i], (data[i,0], data[i,1]))

plt.xlabel(r'$\Delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')
plt.ylabel(r'$\Delta \psi$  cAMP-bound wild-type mHCN2 (mV)')

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

plt.legend(ncol=3)
plt.show()

”在此处输入图像说明“

您需要进行一些次要的字体调整,因此所有内容都适合,也许将长传奇分为两三列。

但是,对于传说,您在图片中显示的内容似乎是多余的。似乎您想指出三个不同的数据。我建议将您的数据分为三种不同类型(代码中的类),并执行三个单独的时间,并将其放入类描述中。
这就是我要做的事情:


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


plt.close('all')


data = 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],
]) 

custom_annotations = ["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"]
class_desc = ['description 1', 'description 2', 'description 3']
dataclass = np.array([0,0,0,0,0,0,1,1,2,2,2,2,2,])
colorclass = ['r','g','b']

for datac in range(3):
    plt.scatter(data[dataclass==datac][:,0],data[dataclass==datac][:,1], c=colorclass[datac], label=class_desc[datac])

for i, point in enumerate(data): 
#     plt.scatter(point[0], point[1], marker='o', label=custom_annotations[i], c=class_colours[i], edgecolors='black', linewidths=1, alpha=0.75)
     plt.annotate(custom_annotations[i], (data[i,0], data[i,1]))

plt.xlabel(r'$\Delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')
plt.ylabel(r'$\Delta \psi$  cAMP-bound wild-type mHCN2 (mV)')

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

plt.legend(fontsize=12)

plt.show()

生产此内容:

”在此处输入图像说明”

First, to produce separate annotations, your problem is that there was no loop. You need to put for i, txt in enumerate(classes): before the plt.annotate

To produce exactly what you want, however, you need to execute the scatter command for each point, and associate a label to each point as follows:

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

figure(figsize=(10, 8), dpi=120)

from scipy.stats import t

plt.close('all')


data = 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],
]) 

custom_annotations = ["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"]

for i, point in enumerate(data): 
    plt.scatter(point[0], point[1], marker='o', label=custom_annotations[i], c=class_colours[i], edgecolors='black', linewidths=1, alpha=0.75)
    plt.annotate(custom_annotations[i], (data[i,0], data[i,1]))

plt.xlabel(r'$\Delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')
plt.ylabel(r'$\Delta \psi$  cAMP-bound wild-type mHCN2 (mV)')

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

plt.legend(ncol=3)
plt.show()

enter image description here

You'll need to make some minor font adjustment so everything fits, perhaps splitting the long legend in two or three columns.

However, for the legend, what you show in the picture seems redundant. It seems like you want to indicate three different groups of data. I recommend to separate your data into the three different types (classes in your code) and execute scatter three separate times and put in the legend the class descriptions.
Here is how I would do it:


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


plt.close('all')


data = 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],
]) 

custom_annotations = ["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"]
class_desc = ['description 1', 'description 2', 'description 3']
dataclass = np.array([0,0,0,0,0,0,1,1,2,2,2,2,2,])
colorclass = ['r','g','b']

for datac in range(3):
    plt.scatter(data[dataclass==datac][:,0],data[dataclass==datac][:,1], c=colorclass[datac], label=class_desc[datac])

for i, point in enumerate(data): 
#     plt.scatter(point[0], point[1], marker='o', label=custom_annotations[i], c=class_colours[i], edgecolors='black', linewidths=1, alpha=0.75)
     plt.annotate(custom_annotations[i], (data[i,0], data[i,1]))

plt.xlabel(r'$\Delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')
plt.ylabel(r'$\Delta \psi$  cAMP-bound wild-type mHCN2 (mV)')

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

plt.legend(fontsize=12)

plt.show()

producing this:

enter image description here

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