I'm trying to create a grapher using matplotlib.pyplot and want to graph a function that comes like a string
My Code is:
import matplotlib.pyplot as mpl
import numpy as np
def plot2D(*args):
mpl.grid(1)
xAxis = np.arange(args[1],args[2],args[3])
def xfunction(x,input):
return eval(input)
print(xfunction(5,args[0]))
mpl.plot(xAxis,xfunction(xAxis,args[0]))
mpl.show()
plot2D("1/(x)",-1,2,0.1)
I want it to plot the function 1/x but it looks like this when it should look like this (desmos). Am I converting the string to a function wrong or can matplotlib even be used to graph functions like that or should I use another library? How would I go about graphing a function like x**2 + y**2 = 1 ? Or functions like sin(x!) ?
There's an intrinsic problem with the function 1/x: it's not defined in 0. Now, in your code one of the values inside the range is unfortunately 0, and thus it messes up the whole thing big time. All you have to do is change the last line of code to shift the range a little bit, and increase the number of steps in order to get more accurate results: plot2D("1/x",-1.01,2,0.02). This is the plot:
If you want to eliminate the nasty line in between you'll have to change the code to split the graph into two.
Related
I am using sympy.plotting.plot to plot my function but, it shows me a different graph then when I plot the same function in my graphic calculator.
My code is:
def S(t):
return (10*sympy.E**(t/12)*((sympy.sin((sympy.pi*t)/24))**2))
sympy.plotting.plot(S(t), xlim=[0,24])
Also, when I just do this,
sympy.plotting.plot(10*sympy.E**(t/12)*((sympy.sin((sympy.pi*t)/24))**2), xlim=[0,24])
it shows a different graph. In my calculator the function has a maximum at approximately t=14 while in python, the graph stops at t=10.
sympy plot function takes a second argument to specify the range to draw on, it's a tuple of (symbol,min,max), while xlim is used to limit the screen after the plot is drawn, it doesn't affect what's being drawn, ie: you can still move the plot to override xlim.
import sympy
t = sympy.sympify('t')
sympy.plotting.plot(10*sympy.E**(t/12)*((sympy.sin((sympy.pi*t)/24))**2),(t,0,24))
I am using a function from a library that does some calculations and makes a plot, directly uses plt.show() to show that plot but only returns numerical output.
I guess this is intended for interactive analysis, but I would like to save the plot within a script, but plt.savefig() only saves an empty file.
Example code:
import numpy as np
import matplotlib.pyplot as plt
def show_plot_but_return_only_values(a):
# do something complicated to input
b = a*3
# show some complicated plot
fig, ax = plt.subplots()
ax.plot(b)
plt.show()
# return new output
return b
b = show_plot_but_return_only_values(np.random.random(7))
# saves empty file
plt.savefig(res_path / 'test.png')
For simple functions I could copy the function from the source code of the library, but this is not always possible/feasible (e.g., references to many other functions).
How can I save this plot?
from matplotlib.pyplot import *
list = [1,3,5,7,2,4,6,8,10]
plot(list)
With my code above, the code will graph with x-axis run from 0 to 8. What should I do if I want my x-axis run from another value? i.e. my x-axis run from 120-128?
You can just use the show() function to show the graph.
Your code should look something like this.
from matplotlib.pyplot import *
list = [1,3,5,7,2,4,6,8,10]
plot(list)
show()
And also, as mentioned in the comments by #BcK, don't use keywords or in-built function names as variable names, finally don't import * , this is not a good practice. You can update your whole program to this:
import matplotlib.pyplot as plt
var_list = [1,3,5,7,2,4,6,8,10] # don't use keywords or inbuilt function names as varible names
plt.plot(var_list)
plt.show() # this is to visualize the plot
I have a complicated method called plotter() which processes some data and produces a matplotlib plot with several components. Due to its complexity I simply want to test that the plot appears. This will confirm that all of the data is processed reasonably and that something gets shown without any errors being thrown. I am not looking to run an image comparison as that's not currently possible for this project.
My function is too complicated to show here, so the following example could be considered instead.
import matplotlib.pyplot as plt
import numpy as np
def plotter():
x = np.arange(0,10)
y = 2*x
fig = plt.plot(x, y)
plotter()
plt.show()
Is there a way to use PyTest to simply assert that a figure appears? If not then solutions using other test frameworks would also be greatly appreciated.
(For context I am using Python 3.)
I often plot a point on a matplotlib plot with:
x = 10
y = 100
plot(x, y, "k*", label="Global Optimum")
legend()
However, this causes the legend to put a star in the legend twice, such that it looks like:
* * Global Optimum
when I really want it to look like:
* Global Optimum
How do I do this?
This should work:
legend(numpoints=1)
BTW, if you add the line
legend.numpoints : 1 # the number of points in the legend line
to your matplotlibrc file, then this will be the new default.
[See also scatterpoints, depending on your plot.]
API: Link to API docs
I like to change my matplotlib rc parameters dynamically in every python script. To achieve this goal I simply use somthing like that at the beginning of my python files.
from pylab import *
rcParams['legend.numpoints'] = 1
This will apply to all plots generated from my python file.
EDIT: For those who do not like to import pylab, the long answer is
import matplotlib as mpl
mpl.rcParams['legend.numpoints'] = 1