Debbie
Debbie

Reputation: 131

Plotting Multiple Power Functions in Python

I am trying to plot three power functions in python, but I am getting a value error. Here is my code. How do I fix this? Please help

Code:

from numpy import *
import math
import matplotlib.pyplot as plt

a = logspace(-4, 2, 10)

m = 0.315*a**-3
r = 0.0000926*a**-2
d = 0.685

plt.plot(a, m, 'r') 
plt.plot(a, r, 'b') 
plt.plot(a, d, 'g') 
plt.show()

The error message:

 ValueError: x and y must have same first dimension, but have shapes (10,) and (1,)

Upvotes: 0

Views: 468

Answers (1)

Jay Patel
Jay Patel

Reputation: 1420

As JohanC suggested you need same number of elements for a and d variable. I just use his suggestions and plot a graph for you if that helps.

import numpy as np
import matplotlib.pyplot as plt

a = np.logspace(-4, 2, 10)

m = 0.315*a**-3
r = 0.0000926*a**-2
d = 0.685

fig = plt.figure(figsize=(4, 3), dpi=200)
plt.plot(a, m, 'r', label='a v/s m') 
plt.plot(a, r, 'b', label='a v/s r') 
# plt.plot(a, d, 'g') # As JohanC suggested you need same number of elements for a and d variable
plt.plot(a, np.full_like(a, d), 'g', label='a v/s d') # alternatively use this
plt.yscale('log')
plt.grid()
plt.legend()

enter image description here

Upvotes: 1

Related Questions