python - 使用 Line2D 在 matplotlib 中绘制线条

标签 python matplotlib

我有数据:

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

我想用 matplotlib.lines.Line2D(xdata, ydata) 绘制它.我试过:

import matplotlib.lines
matplotlib.lines.Line2D(x, y)

但是我该如何显示这条线呢?

最佳答案

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

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()

结果:

enter image description here

关于python - 使用 Line2D 在 matplotlib 中绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28688210/

相关文章:

python - Django - 获取出价最高的用户

python - 使用辅助轴线图制作分类或分组条形图

python - 如何优化算法以更快地找到具有fuzzywuzzy的相似字符串?

python - 如何使用判别函数绘制 3 个类之间的决策边界

python - 您将如何在 Python 中绘制此 3D 可视化图?

python - 在python中分离复数的实部和虚部

python - opencv+python : Assertion failure when findcontours

python-2.7 - 同一图上的 Python 并排箱线图

python - 在 Matplotlib 中使用 LaTex 渲染轴标签

python - 如何使用 tsplot 设置多个标记?