简体   繁体   中英

Drawing line through two points, instead of between (on a log scale, but still a straight line)

I am currently working on a Python API responsible for plotting diagrams and tangents made in a Front-end application using Angular. In this application it is possible to move points to adjust the a line perpendicular to the curve.

When the user thinks this is the way it is supposed to be the diagrams can be exported using matplotlib.

When only drawing between points using code below:

x_values = [tangent.point1.x, tangent.point2.x]
y_values = [tangent.point1.y, tangent.point2.y]
plt.plot(x_values, y_values, scalex=False, scaley=False)

I get normal looking lines as follows在此处输入图像描述

Although I want the lines to keep going till the borders of the plot for if the two points set by the user do not intersect by themselves.

I have tried converting the two points into an equation, and calculate two points from there but without luck (straight line). Also tried using np.linspace(xMin, xMax, 100) resulting on a exponential curve.

If curious, this is what the user would interact with在此处输入图像描述

Question / TLDR: Is there a way to draw the line through the points (indefinetely because I set scalex and scaley to false in the plt.plot)

EDIT:

I have come across in another post ax.axline(p1, p2). This draws an infinite line but does not have a scalex and scaley attribute. This post also referenced storing plt.axes() in a variable and applying them as xlim and ylim after plotting. This results in the lines not being visible??? I mean printing the points to the console. I can see where the line is supposed to be in the diagram, it is just not there.

EDIT 2: I am stoopid and p1 in axline was p1.x and p2.x instead of p1.x and p1.y. This solved by not going out of bounds and being visible again.

To draw infinite lines one can use one of the following (maybe other options aswell)

plt.axlines((x1,y1), (x2,y2))

ax.axlines((x1, y1),(x2, y2))

Having both of the points within the bounding will extend the line to the edges of the bounding box.

If the points were to be outside of the bounding box, the bounding box will scale accordingly to fit the specified points within the box

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