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

how to add the color annotate in a single scatter plot

我需要在单个图中进行颜色注释,如下图(来自两个图的面板)。

所以,我想把下面这张图转过来

对此,根据图上每个点的颜色标注,同时添加图例,如下图

我的完整代码是:

 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循环中的某些内容丢失或写错了,希望的情节应该与post中的图像相同,关于点颜色注释和图例,应该是放置在情节之外。

首先,要生成单独的注释,你的问题是没有循环。您需要在 plt.annotate

之前放置 for i, txt in enumerate(classes):

然而,要准确生成您想要的结果,您需要为每个点执行分散命令,并为每个点关联一个标签,如下所示:

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

您需要对字体进行一些小的调整,以便一切都适合,也许将长图例分成两列或三列。

不过,对于图例来说,你在图片中显示的内容似乎是多余的。似乎您想指示三组不同的数据。我建议将您的数据分成三种不同的类型(代码中的 classes)并分别执行 scatter 三次并在图例中放入 class 描述。 以下是我的做法:


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

制作这个: