I´m trying to create a very simple program using matplotlib and other libraries to develop a way to generate grafics by inputs, however I´m facing some problems. Do you have any ideia?
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (1)
Hello MeiMeiChan,
Your inquiry about creating a Python program for generating function graphs based on user input is an interesting problem. You can certainly accomplish this using libraries like
matplotlib
for generating the plots,sympy
for handling symbolic mathematics, andnumpy
to create numerical ranges. Here's a simple solution to your problem:This Python script will generate a 2D line plot of a function provided by the user. When run, it will ask the user to input a mathematical expression of 'x', such as 'x**2' for a quadratic function or 'sin(x)' for a sine wave. This input is then processed and plotted over the range -10 to 10 on the x-axis.
Specifically,
sympy.sympify()
is used to convert the input string into a symbolic mathematics expression. Thenumpy.linspace()
function generates an array of 400 points evenly spaced between -10 and 10, which are the x-values at which the function will be evaluated.sympy.lambdify()
is then used to convert the symbolic expression into a function that can be evaluated over the numpy arrayx_values
. The resulting y-values are then plotted against the x-values usingmatplotlib.pyplot.plot()
, and the plot is displayed usingmatplotlib.pyplot.show()
.This simple script will need
matplotlib
,numpy
, andsympy
libraries installed in your Python environment. If you don't have them installed, you can do so using pip:This program is a good starting point; you might need to enhance it based on your needs. For instance, this version doesn't handle user input errors, which you might want to consider.
Good luck with your coding!