Last active
January 19, 2022 13:43
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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