matplotlib - Remove undefined number from plot [duplicate]












1
















This question already has an answer here:




  • Omit joining lines in matplotlib plot e.g. y = tan(x)

    4 answers




I'm trying to plot f(x) = 1/x using Python and matplotlib. Currently, my code is:



import numpy as np
import matplotlib.pyplot as plt

def f(x):
return 1/x

t1 = np.arange(-4.0, 4.0, 1)
t2 = np.arange(-4.0, 4.0, 0.02)

figure = plt.figure(1)

ax = figure.add_subplot(111)
ax.tick_params(labeltop = True)
ax.set_ylim([-40,40])

plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')


The output is the following:
The output

It's obviously incorrect, and I'm certain that the problem occurs due to the fact that Python can't calculate the function at point x = 0 (as 1/0 is undefined)



Is it possible to somehow exclude x = 0 from the function?



(There is a question that's pretty much the same as this one, but I was unable to solve the problem with the answers there.)



Thanks!










share|improve this question













marked as duplicate by ImportanceOfBeingErnest matplotlib
Users with the  matplotlib badge can single-handedly close matplotlib questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 15:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • The solution is in the question itself using np.errstate.

    – Scott Boston
    Nov 14 '18 at 15:16
















1
















This question already has an answer here:




  • Omit joining lines in matplotlib plot e.g. y = tan(x)

    4 answers




I'm trying to plot f(x) = 1/x using Python and matplotlib. Currently, my code is:



import numpy as np
import matplotlib.pyplot as plt

def f(x):
return 1/x

t1 = np.arange(-4.0, 4.0, 1)
t2 = np.arange(-4.0, 4.0, 0.02)

figure = plt.figure(1)

ax = figure.add_subplot(111)
ax.tick_params(labeltop = True)
ax.set_ylim([-40,40])

plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')


The output is the following:
The output

It's obviously incorrect, and I'm certain that the problem occurs due to the fact that Python can't calculate the function at point x = 0 (as 1/0 is undefined)



Is it possible to somehow exclude x = 0 from the function?



(There is a question that's pretty much the same as this one, but I was unable to solve the problem with the answers there.)



Thanks!










share|improve this question













marked as duplicate by ImportanceOfBeingErnest matplotlib
Users with the  matplotlib badge can single-handedly close matplotlib questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 15:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • The solution is in the question itself using np.errstate.

    – Scott Boston
    Nov 14 '18 at 15:16














1












1








1









This question already has an answer here:




  • Omit joining lines in matplotlib plot e.g. y = tan(x)

    4 answers




I'm trying to plot f(x) = 1/x using Python and matplotlib. Currently, my code is:



import numpy as np
import matplotlib.pyplot as plt

def f(x):
return 1/x

t1 = np.arange(-4.0, 4.0, 1)
t2 = np.arange(-4.0, 4.0, 0.02)

figure = plt.figure(1)

ax = figure.add_subplot(111)
ax.tick_params(labeltop = True)
ax.set_ylim([-40,40])

plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')


The output is the following:
The output

It's obviously incorrect, and I'm certain that the problem occurs due to the fact that Python can't calculate the function at point x = 0 (as 1/0 is undefined)



Is it possible to somehow exclude x = 0 from the function?



(There is a question that's pretty much the same as this one, but I was unable to solve the problem with the answers there.)



Thanks!










share|improve this question















This question already has an answer here:




  • Omit joining lines in matplotlib plot e.g. y = tan(x)

    4 answers




I'm trying to plot f(x) = 1/x using Python and matplotlib. Currently, my code is:



import numpy as np
import matplotlib.pyplot as plt

def f(x):
return 1/x

t1 = np.arange(-4.0, 4.0, 1)
t2 = np.arange(-4.0, 4.0, 0.02)

figure = plt.figure(1)

ax = figure.add_subplot(111)
ax.tick_params(labeltop = True)
ax.set_ylim([-40,40])

plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')


The output is the following:
The output

It's obviously incorrect, and I'm certain that the problem occurs due to the fact that Python can't calculate the function at point x = 0 (as 1/0 is undefined)



Is it possible to somehow exclude x = 0 from the function?



(There is a question that's pretty much the same as this one, but I was unable to solve the problem with the answers there.)



Thanks!





This question already has an answer here:




  • Omit joining lines in matplotlib plot e.g. y = tan(x)

    4 answers








python matplotlib






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 15:06









werckwerck

6010




6010




marked as duplicate by ImportanceOfBeingErnest matplotlib
Users with the  matplotlib badge can single-handedly close matplotlib questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 15:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by ImportanceOfBeingErnest matplotlib
Users with the  matplotlib badge can single-handedly close matplotlib questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 15:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • The solution is in the question itself using np.errstate.

    – Scott Boston
    Nov 14 '18 at 15:16



















  • The solution is in the question itself using np.errstate.

    – Scott Boston
    Nov 14 '18 at 15:16

















The solution is in the question itself using np.errstate.

– Scott Boston
Nov 14 '18 at 15:16





The solution is in the question itself using np.errstate.

– Scott Boston
Nov 14 '18 at 15:16












1 Answer
1






active

oldest

votes


















1














It's not incorrect. It simply connects each of the points, and when you have one point at + infinity and the other at - infinity at x = 0, you'll get this line connecting the two. The easiest way of plotting it without the line is by plotting two graphs: one for x < 0 and one for x > 0:



import numpy as np
import matplotlib.pyplot as plt

def f(x):
return 1/x

t1a = np.arange(-4.0, 0, 1)
t1b = np.arange( 0, 4.0, 1)
t2a = np.arange(-4.0, 0, 0.02)
t2b = np.arange( 0, 4.0, 0.02)

figure = plt.figure(1)

ax = figure.add_subplot(111)
ax.tick_params(labeltop = True)
ax.set_ylim([-40,40])

plt.plot(t1a, f(t1a), 'bo', t2a, f(t2a), 'k')
plt.plot(t1b, f(t1b), 'bo', t2b, f(t2b), 'k')
plt.show()


resulting in:



enter image description here






share|improve this answer
























  • Perfect, thank you!

    – werck
    Nov 14 '18 at 15:21


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














It's not incorrect. It simply connects each of the points, and when you have one point at + infinity and the other at - infinity at x = 0, you'll get this line connecting the two. The easiest way of plotting it without the line is by plotting two graphs: one for x < 0 and one for x > 0:



import numpy as np
import matplotlib.pyplot as plt

def f(x):
return 1/x

t1a = np.arange(-4.0, 0, 1)
t1b = np.arange( 0, 4.0, 1)
t2a = np.arange(-4.0, 0, 0.02)
t2b = np.arange( 0, 4.0, 0.02)

figure = plt.figure(1)

ax = figure.add_subplot(111)
ax.tick_params(labeltop = True)
ax.set_ylim([-40,40])

plt.plot(t1a, f(t1a), 'bo', t2a, f(t2a), 'k')
plt.plot(t1b, f(t1b), 'bo', t2b, f(t2b), 'k')
plt.show()


resulting in:



enter image description here






share|improve this answer
























  • Perfect, thank you!

    – werck
    Nov 14 '18 at 15:21
















1














It's not incorrect. It simply connects each of the points, and when you have one point at + infinity and the other at - infinity at x = 0, you'll get this line connecting the two. The easiest way of plotting it without the line is by plotting two graphs: one for x < 0 and one for x > 0:



import numpy as np
import matplotlib.pyplot as plt

def f(x):
return 1/x

t1a = np.arange(-4.0, 0, 1)
t1b = np.arange( 0, 4.0, 1)
t2a = np.arange(-4.0, 0, 0.02)
t2b = np.arange( 0, 4.0, 0.02)

figure = plt.figure(1)

ax = figure.add_subplot(111)
ax.tick_params(labeltop = True)
ax.set_ylim([-40,40])

plt.plot(t1a, f(t1a), 'bo', t2a, f(t2a), 'k')
plt.plot(t1b, f(t1b), 'bo', t2b, f(t2b), 'k')
plt.show()


resulting in:



enter image description here






share|improve this answer
























  • Perfect, thank you!

    – werck
    Nov 14 '18 at 15:21














1












1








1







It's not incorrect. It simply connects each of the points, and when you have one point at + infinity and the other at - infinity at x = 0, you'll get this line connecting the two. The easiest way of plotting it without the line is by plotting two graphs: one for x < 0 and one for x > 0:



import numpy as np
import matplotlib.pyplot as plt

def f(x):
return 1/x

t1a = np.arange(-4.0, 0, 1)
t1b = np.arange( 0, 4.0, 1)
t2a = np.arange(-4.0, 0, 0.02)
t2b = np.arange( 0, 4.0, 0.02)

figure = plt.figure(1)

ax = figure.add_subplot(111)
ax.tick_params(labeltop = True)
ax.set_ylim([-40,40])

plt.plot(t1a, f(t1a), 'bo', t2a, f(t2a), 'k')
plt.plot(t1b, f(t1b), 'bo', t2b, f(t2b), 'k')
plt.show()


resulting in:



enter image description here






share|improve this answer













It's not incorrect. It simply connects each of the points, and when you have one point at + infinity and the other at - infinity at x = 0, you'll get this line connecting the two. The easiest way of plotting it without the line is by plotting two graphs: one for x < 0 and one for x > 0:



import numpy as np
import matplotlib.pyplot as plt

def f(x):
return 1/x

t1a = np.arange(-4.0, 0, 1)
t1b = np.arange( 0, 4.0, 1)
t2a = np.arange(-4.0, 0, 0.02)
t2b = np.arange( 0, 4.0, 0.02)

figure = plt.figure(1)

ax = figure.add_subplot(111)
ax.tick_params(labeltop = True)
ax.set_ylim([-40,40])

plt.plot(t1a, f(t1a), 'bo', t2a, f(t2a), 'k')
plt.plot(t1b, f(t1b), 'bo', t2b, f(t2b), 'k')
plt.show()


resulting in:



enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 15:18









NathanNathan

1,239518




1,239518













  • Perfect, thank you!

    – werck
    Nov 14 '18 at 15:21



















  • Perfect, thank you!

    – werck
    Nov 14 '18 at 15:21

















Perfect, thank you!

– werck
Nov 14 '18 at 15:21





Perfect, thank you!

– werck
Nov 14 '18 at 15:21





Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values