简体   繁体   中英

Python Programming: plotting points with mathematical expressions

So i need to be able to plot a point on a graph made by my already coded cartesian coordinate system. The geometry is like: (0,0) is at the top left of the window and as it goes right, the x increases and as it goes down, the y increases so the bottom right corner would be (800, 600).

My cartesian (0,0) is actually on the point (400, 300) and that's where I want my graphs to be aligned to.

My code for getting input, converting it to an expression and graphing these points using small rectangular dots is

expression = input("Enter a mathematical 
for x in range(0, 800):
    y = eval(expression)
    rect(x, y, 2, 2)

My problem is: the code needs to be able to read and graph all normal mathematical expressions properly such as x, x^2, x^3, etc., but on my drawn cartesian plane, the values are actually all positives due to the window's weird quadrant system created by the graphics library.

I'm not getting the correct plotting when my program starts to plot out and map all these coordinates.

Could someone shed some light on what im supposed to do in terms of actually converting these graphics coordinates to match my cartesian plane coordinates?

NOTE every 30 graphics units = 1 tick unit of my cartesian plane.

if your problem is what i think it is, try the following code.

expression = input("Enter a mathematical 
for x in range(0, 800):
    x_val = x-400
    y_val = eval(expression(x_val))
    y = -y_val+300
    rect(x, y, 2, 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM