1

enter image description here

i generated that scatterplot with matplotlib, and i need to add an attribute to each point.

my code is:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
from matplotlib import cm
from colorspacious import cspace_converter
from collections import OrderedDict

cmaps = OrderedDict()

y=[(22.8,19.6),(8.9,13.7,14.7),(1.9,-1.8),(-3,-5.9,-13.4),(-5.7,-6.8)]
x=[-2,-1,0,1,2]
#n=['K464E', 'K472E', 'K464', 'M155E', 'K472', 'M155A', 'Q539A', 'M155R', 'D244A', 'E247A', 'E247R', 'D244K']

plt.title('delta charge vs delta V1/2 mutation-wt')
plt.xlabel('delta charge')
plt.ylabel('delta V1/2 mutation-wt')
fig, plt = plt.scatter(x, y)
for xe, ye in zip(x, y):
    plt.scatter([xe] * len(ye), ye, marker= 'o', cmap='Reds', edgecolors='black', linewidths=1, alpha=0.75)

#plt.annotate('K464E', (x[-2], y[22.8]))

#for i, txt in enumerate('K464E', 'K472E', 'K464', 'M155E', 'K472', 'M155A', 'Q539A', 'M155R', 'D244A', 'E247A', 'E247R', 'D244K'):
    plt.annotate(txt, (x[i], y[i]))

figure(figsize=(8, 6), dpi=180)
plt.savefig('Pure_Jena.png')

which style shall i give to 'marker=...'

UPDATE: i get the following warning without giving any output:

VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. _data = np.array(data, dtype=dtype, copy=copy,

sphero
  • 37
  • 9
  • Does this answer your question? [Matplotlib scatter plot with different text at each data point](https://stackoverflow.com/questions/14432557/matplotlib-scatter-plot-with-different-text-at-each-data-point) – Grekkq Apr 21 '22 at 10:01
  • it is relevant, but since i am completely new to matplotlib, i am not sure if the code i wrote is appropriate to do the job, could you check it out? i modified it in the post. – sphero Apr 21 '22 at 10:13
  • i tried annotate, but it threw an error: NameError: name 'K464E' is not defined – sphero Apr 21 '22 at 10:24
  • Define it as strings `"K464E"` – Grekkq Apr 21 '22 at 10:28

1 Answers1

1

If you want to annotate specific point you can use annotate(), like this:

import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [-2, -1, 0, 1, 2]
plt.scatter(x, y, marker="o")
plt.annotate("custom text", (x[1], y[1]))
plt.savefig("test.png")

output example

Or you can annotate each point using simple for loop:

import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [-2, -1, 0, 1, 2]
custom_annotations = ["test1", "test2", "test3", "test4", "test5"]
plt.scatter(x, y, marker="o")
for i, txt in enumerate(custom_annotations):
    plt.annotate(txt, (x[i], y[i]))
plt.savefig("test.png")

output example You can check annotate details here

Grekkq
  • 679
  • 6
  • 14
  • i tried the first solution and it has been running for a long time without output, and gave me this warning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. _data = np.array(data, dtype=dtype, copy=copy, – sphero Apr 21 '22 at 10:45
  • 1
    I have added working examples with produced output to the answer, your warning suggest problems with `numpy` so it isn't connected with annotation. – Grekkq Apr 21 '22 at 11:18
  • To achieve a better placement of the annotations I usually use [this package](https://github.com/Phlya/adjustText), see if you can adapt some of the examples for your particular problem. – Kastakin Apr 21 '22 at 14:56