I want to Plot V(y axis) vs t(x axis) graph using the below equation at 5 different values of L(shown below)
L= [5,10,15,20,25]
b=0.0032
Equation, (b*V*0.277*t) – (b*L) = log(1+b*V*0.277*t)
code output will be as shown in figure Expected Outcome
Advertisement
Answer
While sympy exposes the plot_implicit
function, the results are far from good. We can use Numpy and Matplotlib to achieve our goal.
The basic idea is that your equation can be written as LHS - RHS = 0
. So, we can create contour plots and select the level 0. But contour plots uses colormaps, so we will have to create solid colormaps:
JavaScript
x
25
25
1
import matplotlib.pyplot as plt
2
import matplotlib.cm as cm
3
from matplotlib.lines import Line2D
4
from matplotlib.colors import ListedColormap
5
import numpy as np
6
7
Lvalues = [5,10,15,20,25]
8
bval = 0.0032
9
10
V = np.linspace(0, 1000)
11
t = np.linspace(0, 10)
12
V, t = np.meshgrid(V, t)
13
f = lambda V, t, b, L: b*V*0.277*t - b*L - np.log(1+b*V*0.277*t)
14
15
colors = cm.tab10.colors
16
handles = []
17
fig, ax = plt.subplots()
18
for L, c in zip(Lvalues, colors):
19
cmap = ListedColormap([c, c])
20
z = f(V, t, bval, L)
21
ax.contour(t, V, z, levels=[0], cmap=cmap)
22
handles.append(Line2D([], [], color=c, label="L = %s" % L))
23
ax.legend(handles=handles)
24
plt.show()
25