Skip to content

Instantly share code, notes, and snippets.

@Hypercoded
Last active January 19, 2022 13:43
Show Gist options
  • Save Hypercoded/68160058fc54c183e63fb4c4fabde5a8 to your computer and use it in GitHub Desktop.
Save Hypercoded/68160058fc54c183e63fb4c4fabde5a8 to your computer and use it in GitHub Desktop.
calbin needs a grapher (but java sucks and is so hard) so here is a python one that is simple
import matplotlib.pyplot as plt
import numpy as np
# 100 linearly spaced numbers
x = np.linspace(-5,5,100)
# the users input
## REFORMATTING
def reformat(string):
string = string.replace("sin","np.sin")
string = string.replace("cos","np.cos")
string = string.replace("tan","np.tan")
string = string.replace("^","**")
return string
def graph(equation):
y = eval(reformat(equation))
# setting the axes at the centre
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# plot the function
plt.plot(x,y, 'r')
# show the plot
plt.savefig("graph.png")
plt.show()
return plt
graph("sin(x)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment