使用 Line2D 在 python 中绘制线条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28688210/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:37:01  来源:igfitidea点击:

Use Line2D to plot line in python

python

提问by user0

I have the data:

我有数据:

x = [10,24,23,23,3]
y = [12,2,3,4,2]

I want to plot it using

我想用它来绘制它

matplotlib.lines.Line2D(xdata, ydata)

matplotlib.lines.Line2D(xdata, ydata)

I use

我用

import matplotlib.lines

matplotlib.lines.Line2D(x, y)

But how do I show the line?

但是我如何显示这条线?

采纳答案by awesoon

You should add the line to a plot and then show it:

您应该将线添加到绘图中,然后显示它:

In [13]: import matplotlib.pyplot as plt

In [15]: from matplotlib.lines import Line2D      

In [16]: fig = plt.figure()

In [17]: ax = fig.add_subplot(111)

In [18]: x = [10,24,23,23,3]

In [19]: y = [12,2,3,4,2]

In [20]: line = Line2D(x, y)

In [21]: ax.add_line(line)
Out[21]: <matplotlib.lines.Line2D at 0x7f4c10732f60>

In [22]: ax.set_xlim(min(x), max(x))
Out[22]: (3, 24)

In [23]: ax.set_ylim(min(y), max(y))
Out[23]: (2, 12)

In [24]: plt.show()

The result:

结果:

enter image description here

在此处输入图片说明

回答by benjimin

The more common approach (not exactly what the questioner asked) is to use the plotinterface. This involves Line2D behind the scenes.

更常见的方法(不完全是提问者所问的)是使用绘图界面。这涉及幕后的 Line2D。

>>> x = [10,24,23,23,3]
>>> y = [12,2,3,4,2]
>>> import matplotlib.pyplot as plt
>>> plt.plot(x,y)
[<matplotlib.lines.Line2D object at 0x7f407c1a8ef0>]
>>> plt.show()