Home > Enterprise >  Getting rid of a specific line in plots
Getting rid of a specific line in plots

Time:06-23

x = [0.0000000,0.0082707,0.0132000, 0.0255597, 0.0503554, 0.0751941, 0.1000570, 
     0.1498328, 0.1996558, 0.2495240, 0.2994312, 0.3993490, 0.4993711, 0.5994664, 
     0.6996058, 0.7997553, 0.8998927, 0.9499514, 1.0000000, 0.0000000, 0.006114, 
     0.0062188, 0.0087532, 0.0138088, 0.0264052, 0.0515127, 0.0765762, 0.1016176, 
     0.1516652, 0.2016828, 0.2516733, 0.3016387, 0.4015163, 0.5013438, 0.6011363, 
     0.7008976, 0.8006328, 0.9003380, 0.9501740, 1.0000000] 

y = [0.0000000, 0.0233088, 0.0298517, 0.0425630, 0.0603942, 0.0739301, 0.0850687, 
     0.1023515, 0.1149395, 0.1230325, 0.1272298, 0.1253360, 0.1130538, 0.0934796, 
     0.0695104, 0.0445423, 0.0207728, 0.0098870, 0.0000000, 0.0000000, -.0208973, 
     -.0210669, -.0247377, -.0307807, -.0416431, -.0548774, -.0637165, -.0703581, 
     -.0801452, -.0869356, -.0910290, -.0926252, -.0905235, -.0834273, -.0728351, 
     -.0591463, -.0428603, -.0235778, -.0122883, 0.0000000]

plt.plot(x,y)

This is a data point I have web scraped from a website. If I plot this there is a line that crosses from (0,0) to (1,0) which I don't want. I've tried manually removing points and seeing what points I need to remove but I can't seem to get it to work. There are two pairs of the same points in this data point. How can you remove the line that crosses from (0,0) to (1,0) and can you automate it by using if statement?

CodePudding user response:

If you look carefully at your data, there appears to be a discontinuity. Your x values start going up, then goes back to 0, and goes up again. This discontinuity is creating the horizontal line that's bothering you. You can loop through x and y, detect when the values of x drop instead of increase, find this index and separate the two lists. However, using np.diff can help with this too. In this specific case, there's only one drop, so you have only one negative diff value. Here's my code and the graph I got. It might not generalize to any number of discontinuities, but it fixes your case.

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

x = [0.0000000,0.0082707,0.0132000, 0.0255597, 0.0503554, 0.0751941, 0.1000570, 
     0.1498328, 0.1996558, 0.2495240, 0.2994312, 0.3993490, 0.4993711, 0.5994664, 
     0.6996058, 0.7997553, 0.8998927, 0.9499514, 1.0000000, 0.0000000, 0.006114, 
     0.0062188, 0.0087532, 0.0138088, 0.0264052, 0.0515127, 0.0765762, 0.1016176, 
     0.1516652, 0.2016828, 0.2516733, 0.3016387, 0.4015163, 0.5013438, 0.6011363, 
     0.7008976, 0.8006328, 0.9003380, 0.9501740, 1.0000000] 

y = [0.0000000, 0.0233088, 0.0298517, 0.0425630, 0.0603942, 0.0739301, 0.0850687, 
     0.1023515, 0.1149395, 0.1230325, 0.1272298, 0.1253360, 0.1130538, 0.0934796, 
     0.0695104, 0.0445423, 0.0207728, 0.0098870, 0.0000000, 0.0000000, -.0208973, 
     -.0210669, -.0247377, -.0307807, -.0416431, -.0548774, -.0637165, -.0703581, 
     -.0801452, -.0869356, -.0910290, -.0926252, -.0905235, -.0834273, -.0728351, 
     -.0591463, -.0428603, -.0235778, -.0122883, 0.0000000]

discont = np.diff(x).argmin()
x1 = x[:discont 1]
y1 = y[:discont 1]
x2 = x[discont 1:]
y2 = y[discont 1:]

plt.plot(x1, y1)
plt.plot(x2, y2)
plt.show()

enter image description here

  • Related