Python 如何用matplotlib画一条线?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36470343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw a line with matplotlib?
提问by Alexey
I cannot find a way to draw an arbitrary line with matplotlib
Python library. It allows to draw horizontal and vertical lines (with matplotlib.pyplot.axhline
and matplotlib.pyplot.axvline
, for example), but i do not see how to draw a line through two given points (x1, y1)
and (x2, y2)
. Is there a way? Is there a simple way?
我找不到用matplotlib
Python 库绘制任意线的方法。它允许绘制水平线和垂直线(例如,使用matplotlib.pyplot.axhline
和matplotlib.pyplot.axvline
),但我不知道如何通过两个给定点(x1, y1)
和绘制一条线(x2, y2)
。有办法吗?有没有简单的方法?
采纳答案by Alejandro
I was checking how ax.axvline does work, and I've written a small function that resembles part of its idea:
我正在检查 ax.axvline 是如何工作的,并且我编写了一个类似于其部分想法的小函数:
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
def newline(p1, p2):
ax = plt.gca()
xmin, xmax = ax.get_xbound()
if(p2[0] == p1[0]):
xmin = xmax = p1[0]
ymin, ymax = ax.get_ybound()
else:
ymax = p1[1]+(p2[1]-p1[1])/(p2[0]-p1[0])*(xmax-p1[0])
ymin = p1[1]+(p2[1]-p1[1])/(p2[0]-p1[0])*(xmin-p1[0])
l = mlines.Line2D([xmin,xmax], [ymin,ymax])
ax.add_line(l)
return l
So, if you run the following code you will realize how does it work. The line will span the full range of your plot (independently on how big it is), and the creation of the line doesn't rely on any data point within the axis, but only in two fixed points that you need to specify.
因此,如果您运行以下代码,您将了解它是如何工作的。该线将跨越您的绘图的整个范围(与它的大小无关),并且该线的创建不依赖于轴内的任何数据点,而仅依赖于您需要指定的两个固定点。
import numpy as np
x = np.linspace(0,10)
y = x**2
p1 = [1,20]
p2 = [6,70]
plt.plot(x, y)
newline(p1,p2)
plt.show()
回答by Reblochon Masque
This will draw a line that passes through the points (-1, 1) and (12, 4), and another one that passes through the points (1, 3) et (10, 2)
这将绘制一条穿过点 (-1, 1) 和 (12, 4) 的线,以及穿过点 (1, 3) 和 (10, 2) 的另一条线
x1 are the x coordinates of the points for the first line, y1 are the y coordinates for the same -- the elements in x1 and y1 must be in sequence.
x1 是第一条线的点的 x 坐标,y1 是相同点的 y 坐标——x1 和 y1 中的元素必须按顺序排列。
x2 and y2 are the same for the other line.
另一行的 x2 和 y2 相同。
import matplotlib.pyplot as plt
x1, y1 = [-1, 12], [1, 4]
x2, y2 = [1, 10], [3, 2]
plt.plot(x1, y1, x2, y2, marker = 'o')
plt.show()
I suggest you spend some time reading / studying the basic tutorials found on the very rich matplotlib website to familiarize yourself with the library.
我建议您花一些时间阅读/学习在非常丰富的 matplotlib 网站上找到的基本教程,以熟悉该库。
What if I don't want line segments?
如果我不想要线段怎么办?
There are no direct ways to have lines extend to infinity... matplotlib will either resize/rescale the plot so that the furthest point will be on the boundary and the other inside, drawing line segments in effect; or you must choose points outside of the boundary of the surface you want to set visible, and set limits for the x and y axis.
没有直接的方法可以将线延伸到无穷大……matplotlib 将调整/重新调整绘图的大小,使最远的点位于边界上,另一个位于内部,从而有效地绘制线段;或者您必须选择要设置为可见的曲面边界之外的点,并为 x 和 y 轴设置限制。
As follows:
如下:
import matplotlib.pyplot as plt
x1, y1 = [-1, 12], [1, 10]
x2, y2 = [-1, 10], [3, -1]
plt.xlim(0, 8), plt.ylim(-2, 8)
plt.plot(x1, y1, x2, y2, marker = 'o')
plt.show()
回答by Ahaha
Just want to mention another option here.
只是想在这里提到另一个选择。
You can compute the coefficients using numpy.polyfit(), and feed the coefficients to numpy.poly1d(). This function can construct polynomials using the coefficients, you can find more examples here
您可以使用 numpy.polyfit() 计算系数,并将系数提供给 numpy.poly1d()。此函数可以使用系数构造多项式,您可以在此处找到更多示例
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.poly1d.html
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.poly1d.html
Let's say, given two data points (-0.3, -0.5) and (0.8, 0.8)
假设给定两个数据点 (-0.3, -0.5) 和 (0.8, 0.8)
import numpy as np
import matplotlib.pyplot as plt
# compute coefficients
coefficients = np.polyfit([-0.3, 0.8], [-0.5, 0.8], 1)
# create a polynomial object with the coefficients
polynomial = np.poly1d(coefficients)
# for the line to extend beyond the two points,
# create the linespace using the min and max of the x_lim
# I'm using -1 and 1 here
x_axis = np.linspace(-1, 1)
# compute the y for each x using the polynomial
y_axis = polynomial(x_axis)
fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 1, 1])
axes.set_xlim(-1, 1)
axes.set_ylim(-1, 1)
axes.plot(x_axis, y_axis)
axes.plot(-0.3, -0.5, 0.8, 0.8, marker='o', color='red')
Hope it helps.
希望能帮助到你。