Reputation: 1
Why do I keep getting errors
import matplotlib.pyplot as plt
import numpy as np
u = input('Equation:')
u = u.replace('y', '')
u = u.replace('=', '')
u = u.replace('^', ' ** ')
u = u.replace('+', ' + ')
u = u.replace('-', ' - ')
u = u.replace('x', ' * x', )
u = u.replace('+*', ' + ')
u = u.replace('-*', ' - ')
u = u.replace('***', ' ** ')
x = np.array(range(100))
y = u
plt.plot(x, y)
plt.show()
The error message:
Traceback (most recent call last): File "/Users/xxxxx/PycharmProjects/PyShaps/j,g.py", line 17, in plt.plot(x, y)
File "/Users/xxxxx/PycharmProjects/PyShaps/venv/lib/python3.9/site-packages/matplotlib/pyplot.py", line 2757, in plot return gca().plot(
File "/Users/xxxxx/PycharmProjects/PyShaps/venv/lib/python3.9/site-packages/matplotlib/axes/_axes.py", line 1632, in plot lines = [*self._get_lines(*args, data=data, **kwargs)]
File "/Users/xxxxx/PycharmProjects/PyShaps/venv/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 312, in call yield from self._plot_args(this, kwargs)
File "/Users/xxxxx/PycharmProjects/PyShaps/venv/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 449, in _plot_args linestyle, marker, color = _process_plot_format(fmt)
File "/Users/xxxxx/PycharmProjects/PyShaps/venv/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 184, in _process_plot_format raise ValueError(
ValueError: Illegal format string "4 * x"; two marker symbols
Process finished with exit code 1
Upvotes: 0
Views: 325
Reputation: 640
Try using equation types and asking user to input real numbers in equation
x = np.array((range(100))
print('equation types')
print('1) y=ax+b')
print('2) y=ax^2 + bx + c')
#...etc.
eq = int(input('equation type number: '))
if eq == 1:
a = float(input('a'))
b = float(input('b'))
y = a*x+b
if eq == 2:
a = float(input('a'))
b = float(input('b'))
c = float(input('c'))
y = ax^2 + bx + c
#...etc.
plt.plot(x, y)
plt.show()
Upvotes: 0