Difficulties using matplotlib plot method


Very recently I have been tasked with ploting a derivative using Python and matplotlib. This is my code:

plt.plot(x, funcion)

The resulting plot is this:
Plot generated in Python

At first sight, the graph looks okay, but is not correct, given that the graph is suposed to look like this:
Correct plot

How can I fix this? I have tried changing the linespace a bunch of times, and the results are the same.

I’ve tried to plot a derivate in matplotlib and the graph is incorrect.

Asked By: Luis Gomez



The problem is not with matplotlib, but instead the range of x values you chose. If you look at your own picture, the xvalues are ranging from around -2 to 2, so if I do the same and play with the plotting bounds I get:

import matplotlib.pyplot as plt
import numpy as np

plt.plot(x, funcion)

plt.axvline(0, color = 'k')
plt.axhline(0, color = 'k')

plt.xlim([-0.8, 1.4])
plt.ylim([-3.5, 3])

which gives


Answered By: t.o.
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.