Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python doesn't plot the graph

i have a little problem with matplotlib and python. So my problem is the line don’t appear in the plot. I am trying to make a graph of a custom function. My code is here bellow:

import scipy
from numpy import arange
import matplotlib.pyplot as plt


def integral_function(x, a, b, c): # defines the function
    return a*x**2 + b*x + c
for r in arange(0,5,0.01): # defines the range and step
    x = r
y =scipy.integrate.quad(integral_function, 0, x, args=(2,3,4))[0] # computes the integral for the first list of constants
y1 = scipy.integrate.quad(integral_function, 0, x, args=(2,1,1))[0] # computes the integral for the second list of constants
plt.plot(r, y) # plots the results of the first list of constants
plt.plot(r, y1) # plots the results of the  second list of constants
plt.xlabel("r values")
plt.ylabel("values of integration")
plt.legend(["for a,b,c = 2,3,4","for a,b,c = 2,1,1"])
plt.title("integration of ax^2 + bx + c")
plt.show()

Everytime that i run the code the graph appears without the lines. Please could someone can help me ? Thank you for the attention 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is because you are trying to plot a point using plot() function. You can either use scatter() function to draw the point or pass a color and dot sign to plot() function.

import scipy
from numpy import arange
import matplotlib.pyplot as plt


def integral_function(x, a, b, c): # defines the function
    return a*x**2 + b*x + c

for r in arange(0,5,0.01): # defines the range and step
    x = r
    y =scipy.integrate.quad(integral_function, 0, x, args=(2,3,4))[0] # computes the integral for the first list of constants
    y1 = scipy.integrate.quad(integral_function, 0, x, args=(2,1,1))[0] # computes the integral for the second list of constants
    plt.plot(r, y,'b.') # plots the results of the first list of constants
    plt.plot(r, y1,'r.') # plots the results of the  second list of constants
plt.xlabel("r values")
plt.ylabel("values of integration")
plt.legend(["for a,b,c = 2,3,4","for a,b,c = 2,1,1"])
plt.title("integration of ax^2 + bx + c")
plt.show()

The output will be :

enter image description here

Is there a better way to draw this plot? Absolutely! It’s recommended to calculate all values before plotting, then plotting all values at once.

import scipy
from numpy import arange
import matplotlib.pyplot as plt


def integral_function(x, a, b, c): # defines the function
    return a*x**2 + b*x + c
y = []
y1 = []
for r in arange(0,5,0.01): # defines the range and step
    x = r
    y.append(scipy.integrate.quad(integral_function, 0, x, args=(2,3,4))[0]) # computes the integral for the first list of constants
    y1.append(scipy.integrate.quad(integral_function, 0, x, args=(2,1,1))[0]) # computes the integral for the second list of constants

    
r = arange(0,5,0.01)
plt.plot(r, y,'b')
plt.plot(r, y1,'r')
plt.xlabel("r values")
plt.ylabel("values of integration")
plt.legend(["for a,b,c = 2,3,4","for a,b,c = 2,1,1"])
plt.title("integration of ax^2 + bx + c")
plt.show()

And here is the output of recommended method :

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading