1

I am producing data and storing them in a numpy array. I am also calculating a variable and storing those variables in another array. They have different shapes and ndims but they are plottable since I tried it with another simple code with the same logic.

import numpy as np
import matplotlib.pyplot as plt

a = np.array([0.05718623, 0.05446883, 0.04395619, 0.03004849])
b = np.array([[0.61745], [0.45825], [0.80061], [0.3719]])
plt.yscale('log')
plt.xscale('log')
plt.plot(a,b)
plt.show()

enter image description here

When I want to plot two arrays with my stored data with the following code:

works = [Data]
probability = [Data2]
plt.yscale('log')
plt.xscale('log')
plt.plot(probability, works)

I get this:

enter image description here

However when I place a 'bo' in the plt.plot() I get this:

works = [Data]
probability = [Data2]
plt.yscale('log')
plt.xscale('log')
plt.plot(probability, works, 'bo')

enter image description here

So I don't want blue spots. Instead of that I want a single curve like blue spots. What am I missing here? What is the problem behind this ? Or this question needs how I produce my data ?

PS: Data.ndim = (1000,) Data2.ndim = (1000,1)

omerS
  • 817
  • 2
  • 9
  • 21

1 Answers1

0

plt.plot(probability, works) connects all dotes with line segments in the order listed in the input array.

For example, plt.plot([1,2,3], [1,4,9]) makes a nice piece-wise line connections between (1,1), (2,4) and (3, 9) in the given order. On the other hand, plt.plot([1,3,2], [1,9,4]) would join (1,1) with (3, 9) with a line segment and (3, 9) and (2, 4) with a line segment -- we get an ugly plot in the later case.

As JohanC suggested, you need to sort the data. Sorting just the first array will ruin the correspondence: Data[0] corresponds to Data2[0]. Therefore, we need to sort Data, but keep the correspondence. zip(Data, Data2) freezes the correspondence. sorted sorts by the first index first in Python, i.e., it would sort Data. Finally, we need to undo zip and get back our two arrays. zip(*sorted(zip(Data, Data2)) does exactly that.

Kate Melnykova
  • 1,863
  • 1
  • 5
  • 17