Python中直线(数字线)上的图形点

JDG*_*DGD 7 python charts graph matplotlib

试图弄清楚在python中绘制数字线上点的最佳方法是什么.基本上尝试制作类似于下图的内容:

指向数字线

我一直在尝试使用Matplotlib来做到这一点,但似乎无法弄明白.有人知道我可以使用的包裹或任何东西吗?

Mol*_*lly 11

我不知道具体的包装,但你可以使用hlines,vlinesplot在Matplotlib中做这样的事情.

import matplotlib.pyplot as plt

# set up the figure
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim(0,10)
ax.set_ylim(0,10)

# draw lines
xmin = 1
xmax = 9
y = 5
height = 1

plt.hlines(y, xmin, xmax)
plt.vlines(xmin, y - height / 2., y + height / 2.)
plt.vlines(xmax, y - height / 2., y + height / 2.)

# draw a point on the line
px = 4
plt.plot(px,y, 'ro', ms = 15, mfc = 'r')

# add an arrow
plt.annotate('Price five days ago', (px,y), xytext = (px - 1, y + 1), 
              arrowprops=dict(facecolor='black', shrink=0.1), 
              horizontalalignment='right')

# add numbers
plt.text(xmin - 0.1, y, '80', horizontalalignment='right')
plt.text(xmax + 0.1, y, '115', horizontalalignment='left')

plt.axis('off')
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Edw*_*vey 6

来吧,来自 matplotlib 的官方示例,包括示例代码和所有内容。要在线上绘制标记点,只需使用 y 坐标为 0的普通散点图即可

https://matplotlib.org/examples/ticks_and_spines/tick-locators.html

在此处输入图片说明