i am trying to generate a plot with annotations, the issue is that i have for each x value multiple y values correspond to it, so in order to keep x and y similar in size, i wrote the following:
x = [-2,-1,0,1,2]
y = [(22.8,19.6),(8.9,13.7,14.7),(1.9,-1.8),(-3,-5.9,-13.4),(-5.7,-6.8)]
however, i am constantly getting the follwoing warning:
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,
my full code:
import matplotlib.pyplot as plt
import numpy as np
x = [-2,-1,0,1,2]
y = [(22.8,19.6),(8.9,13.7,14.7),(1.9,-1.8),(-3,-5.9,-13.4),(-5.7,-6.8)]
custom_annotations = ["K464E", "K472E", "K464", "M155E", "K472", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]
plt.scatter(x, y, marker="o")
for i, txt in enumerate(custom_annotations):
plt.annotate(txt, (x[i], y[i]))
plt.savefig("1.png")
CodePudding user response:
You want to build all x-to-y pairs of your data. Then you can easily fill an array with this data, plot it and annotate it. This means that you have to give your reoccurring x-values multiple times.
import matplotlib.pyplot as plt
import numpy as np
plt.close('all')
# We define the data as a 12 x 2 array
# data[:,0] are the x values
# data[:,1] are the y values
data = np.array([
[-2, 22.8],
[-2, 19.6],
[-1, 8.9],
[-1, 13.7],
[-1, 14.7,],
[ 0, 1.9],
[ 0, -1.8],
[ 1, -3],
[ 1, -5.9],
[ 1, -13.4],
[ 2, -5.7],
[ 2, -6.8],
])
custom_annotations = ["K464E", "K472E", "K464", "M155E", "K472", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]
plt.scatter(data[:,0], data[:,1], marker="o")
for i, txt in enumerate(custom_annotations):
plt.annotate(txt, (data[i,0], data[i,1]))
CodePudding user response:
From what I understand, the np arrays must be declared implicitely by matplotlib in your code.
To solve this and remove the warning, you can just declare x and y as numpy array while making sure that the dtype of y is an object (an object can have a varying size here).
Like this :
x = np.array([-2,-1,0,1,2])
y = np.array(
[(22.8,19.6),(8.9,13.7,14.7),(1.9,-1.8),(-3,-5.9,-13.4),(-5.7,-6.8)],
dtype = object)