Plot with python
Question:
Hello i know that this way to plot in matlab works:
subplot(2,2,[1, 2])
plot(Tabsauv(:, 2:2:2*Ntracks), Tabsauv(:, 3:2:(2*Ntracks+1)),couleur,'LineWidth',2, 'MarkerSize', 2)
grid('on')
hold on
When Tabasauv is a table of values.
I’m a new beginner in python, I tried to plot this in this way with python but it still not working, any idea?
80 Ntracks=5
81 fig= plt.figure()
82 ax=fig.add_subplot(1,1,1)
83
84 data_1=np.array([:,2:2:(2*Ntracks)])
85 data_2=np.array([:,3:2:(2*Ntracks+1])
86 points = data[:,2:4]
87
88 color = np.sqrt((points**2).sum(axis = 1))/np.sqrt(2.0)
89 rgb = plt.get_cmap('jet')(color)
90
91 ax.scatter(data_1, data_2, color = rgb)
92 plt.show()
I got this error caus’ i don’t know how to translate it in python:
data_1=np.arange([:,2:2:(2*Ntracks)])
^
SyntaxError: invalid syntax
Thank you.
Answers:
You probably mean
data_1 = data[:,2:(2*Ntracks):2]
instead in your code, and similarly for data_2
.
A few more things to note:
-
In numpy
indices start from 0. data[:,1:]
will skip the first column. Watch out for off-by-one error when doing the translation.
-
Slicing works slightly differently. The stride is specified by the last number, which is 2 in the above case, not the middle number (different from the Matlab
convention).
In matlab, you can generate arrays with ":".
In python, you can create arrays using range() function or using arange (in numpy)
so,
a = 3:5
in matlab is just the same as
a = np.arange(3,5) #or
a = np.array(range(3,5))
in Python.
Anyway, you might want to look at this page Link in order to translate you code thought patterns from Matlab to Numpy.
Hello i know that this way to plot in matlab works:
subplot(2,2,[1, 2])
plot(Tabsauv(:, 2:2:2*Ntracks), Tabsauv(:, 3:2:(2*Ntracks+1)),couleur,'LineWidth',2, 'MarkerSize', 2)
grid('on')
hold on
When Tabasauv is a table of values.
I’m a new beginner in python, I tried to plot this in this way with python but it still not working, any idea?
80 Ntracks=5
81 fig= plt.figure()
82 ax=fig.add_subplot(1,1,1)
83
84 data_1=np.array([:,2:2:(2*Ntracks)])
85 data_2=np.array([:,3:2:(2*Ntracks+1])
86 points = data[:,2:4]
87
88 color = np.sqrt((points**2).sum(axis = 1))/np.sqrt(2.0)
89 rgb = plt.get_cmap('jet')(color)
90
91 ax.scatter(data_1, data_2, color = rgb)
92 plt.show()
I got this error caus’ i don’t know how to translate it in python:
data_1=np.arange([:,2:2:(2*Ntracks)])
^
SyntaxError: invalid syntax
Thank you.
You probably mean
data_1 = data[:,2:(2*Ntracks):2]
instead in your code, and similarly for data_2
.
A few more things to note:
-
In
numpy
indices start from 0.data[:,1:]
will skip the first column. Watch out for off-by-one error when doing the translation. -
Slicing works slightly differently. The stride is specified by the last number, which is 2 in the above case, not the middle number (different from the
Matlab
convention).
In matlab, you can generate arrays with ":".
In python, you can create arrays using range() function or using arange (in numpy)
so,
a = 3:5
in matlab is just the same as
a = np.arange(3,5) #or
a = np.array(range(3,5))
in Python.
Anyway, you might want to look at this page Link in order to translate you code thought patterns from Matlab to Numpy.