-1
import matplotlib.pyplot as plt
import numpy as np
import csv as csv

    x=[]
    y=[]

with open('DTS_02.csv', 'r') as csvfile:

    plots=csv.reader(csvfile, delimiter=';')

    for row in plots:
        x.append(float(row[1]))
        y.append(float(row[2]))

plt.plot(x,y, label='Hello,World')
plt.xlabel('depth')
plt.ylabel('temperature')
plt.grid()
plt.title('1-e6')
plt.show()

picture --> [1]: https://i.stack.imgur.com/9T4lP.png

So I am trying to execute this one and my sample contains 1 million rows. There are two 2 problems 1.Why do I get such a thick line? 2.Why there is a line connecting starting and end point? Additionally, what would be your advice on improving this code(Without shifting to a new module)...

1 Answers1

0

A1: because you are plotting a line, and I suspect x is not sorted.

A2. See question 1.

A3: use the following and see how it works (since you are using numpy):

x = np.array(x)
y = np.array(y)
plt.plot(x[x.argsort()], y[x.argsort()], label='Hello,World')

EDIT: If you are having a lot of noise, you can try plotting less points, e.g.:

plt.plot(x[x.argsort()][::3], y[x.argsort()][::3], label='Hello,World')    #each 3 points

Or plot a moving average (see here)

Tarifazo
  • 4,118
  • 1
  • 9
  • 22
  • Unfortunately,it does not work.But would not it change my curve if I sort x and y?And also is there anyway of avoiding A2? – Arif Rustamov Jan 16 '19 at 19:04
  • No, it doesn't. I'm not suggesting you sort both the arrays. I'm suggesting you plot `x-y` in increasing order of `x`. The `[x.argsort()]`does that without changing the arrays. Do you still get the same result this way? In that case, can you post a sample of your data/plot? Also, try plotting a scatter with `plt.plot(x,y,'bo')` – Tarifazo Jan 16 '19 at 19:11
  • You can see the previous one, I will now post the one that you said – Arif Rustamov Jan 16 '19 at 19:13
  • It says list has no attribute to 'argsort' – Arif Rustamov Jan 16 '19 at 19:15
  • With 'bo' now there is no A2 but the line is even thicker now – Arif Rustamov Jan 16 '19 at 19:17
  • did you put the `x=np.array(x)` and `y=np.array(y)`? – Tarifazo Jan 16 '19 at 19:28
  • Yes I did,but still no progress – Arif Rustamov Jan 16 '19 at 19:32
  • Apart from that,don't you know how can I can combine other graphs with this one? Creating new subplots I mean – Arif Rustamov Jan 16 '19 at 19:33
  • Yes, check [these tutorials](https://matplotlib.org/users/pyplot_tutorial.html#working-with-multiple-figures-and-axes) – Tarifazo Jan 16 '19 at 19:35
  • You probably have a problem with noise in your data.. There's no much you can do, except try to smooth it using an fft or moving average. Or (easier), try plotting less points, I'll add this to the answer – Tarifazo Jan 16 '19 at 19:36