Write a program to draw a text-based graph of a mathematical function f(x). Use
ID: 652411 • Letter: W
Question
Write a program to draw a text-based graph of a mathematical function f(x). Use axis limits of -10 to 10 and only plot discrete points i.e. points with integer value ordinates.
Sample I/O:
Enter a function f(x):
x+2
please see picture
Use nested loops to print the entire area of the graph (i.e. an outer loop for rows and an inner one for columns), keeping track of the current x and y values. Whenever the (rounded) value of the function f(x), entered by the user, is equal to the current y value, output "o" (small letter Oh), otherwise, output either the appropriate axis character or a space.
NOTE: Remember to import math to enable the use of some mathematical functions. How should the program support the entering of arbitrary functions?
- Obtain user input in the form of a string, then within the inner loop,
- whenever f(x) is to be calculated, use the Python
Explanation / Answer
Anfrom matplotlib import pyplot
from numpy import arange
import bisect
def scatterplot(x,y):
pyplot.plot(x,y,'b.')
pyplot.xlim(min(x)-1,max(x)+1)
pyplot.ylim(min(y)-1,swer
max(y)+1)
pyplot.show()
def barplot(labels,data):
pos=arange(len(data))
pyplot.xticks(pos+0.4,labels)
pyplot.bar(pos,data)
pyplot.show()
def histplot(data,bins=None,nbins=5):
if not bins:
minx,maxx=min(data),max(data)
space=(maxx-minx)/float(nbins)
bins=arange(minx,maxx,space)
binned=[bisect.bisect(bins,x) for x in data]
l=['%.1f'%x for x in list(bins)+[maxx]] if space<1 else [str(int(x)) for x in list(bins)+[maxx]]
displab=[x+'-'+y for x,y in zip(l[:-1],l[1:])]
barplot(displab,[binned.count(x+1) for x in range(len(bins))])
def barchart(x,y,numbins=5):
datarange=max(x)-min(x)
bin_width=float(datarange)/numbins
pos=min(x)
bins=[0 for i in range(numbins+1)
for i in range(numbins):
bins[i]=pos
pos+=bin_width
bins[numbins]=max(x)+1
binsum=[0 for i in range(numbins)]
bincount=[0 for i in range(numbins)]
binaverage=[0 for i in range(numbins)]
for i in range(numbins):
for j in range(len(x)):
if x[j]>=bins[i] and x[j]<bins[i+1]:
bincount[i]+=1
binsum[i]+=y[j]
for i in range(numbins):
binaverage[i]=float(binsum[i])/bincount[i]
barplot(range(numbins),binaverage)
def piechart(labels,data):
fig=pyplot.figure(figsize=(7,7))
pyplot.pie(data,labels=labels,autopct='%1.2f%%')
pyplot.show()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.