0

I have created a graph of three curves in Python where I want the colors to be different for each curve. How can I put different colors on each curve? For example green, blue and red, instead of red on all three.

Following is the Python code:

import matplotlib.pyplot as plt
import math

a = 9.8  # Acceleration due to gravity,  m/s^2
theta = []
v = 50.0
m = 10.0
c = 1.29
vt = (m * 9.8) / c  # terminal velocity #151.93
time = []
horizontal_range = []
vertical_range = []

for th in range(0,60,15):
    for t in range(0, 50):
        var = (-a * t) / vt
        z = (vt / 9.8) * (v * 0.866 + vt) * (1.0 - (2.718 ** var)) - vt * t
        x = ((1.0 - math.exp(var)) * (v * vt * math.cos(th))) / a
        horizontal_range.append(x)
        vertical_range.append(z)
        time.append(t)

    theta.append(th)

plt.plot(horizontal_range, vertical_range, color='red')
plt.xlabel('x')
plt.ylabel('z')
plt.ylim(0)
plt.xlim(0)
plt.title('Projectile')
plt.show()

enter image description here

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Instead of the weird `2.718 ** var`, try to use `math.exp(var)`. Also, if you want to do a lot of calculations, and win on speed and ease-of-writing formulas, you really should check out [numpy](https://numpy.org/doc/stable/user/absolute_beginners.html). – JohanC Feb 14 '22 at 09:25

1 Answers1

1

You should store the curves in separate lists and plot each of them individually to get a different color :

import matplotlib.pyplot as plt
import math

a = 9.8  # Acceleration due to gravity,  m/s^2
theta = []
v = 50.0
m = 10.0
c = 1.29
vt = (m * 9.8) / c  # terminal velocity #151.93
time = {}
horizontal_range = {}
vertical_range = {}

for th in range(0,60,15):
    horizontal_range[th] = []
    vertical_range[th] = []
    time[th] =[]
    for t in range(0, 50):
        var = (-a * t) / vt
        z = (vt / 9.8) * (v * 0.866 + vt) * (1.0 - (2.718 ** var)) - vt * t
        x = ((1.0 - math.exp(var)) * (v * vt * math.cos(th))) / a
        horizontal_range[th].append(x)
        vertical_range[th].append(z)

    theta.append(th)

print(horizontal_range.keys())

for th in range(0,60,15):
    plt.plot(horizontal_range[th], vertical_range[th])
plt.xlabel('x')
plt.ylabel('z')
plt.ylim(0)
plt.xlim(0)
plt.title('Projectile')
plt.show()

Here is the image with the base matplotlib colors

endive1783
  • 827
  • 1
  • 8
  • 18