在Python中的直线(数字线)上绘制点 [英] Graph point on straight line (number line) in Python

查看:3541
本文介绍了在Python中的直线(数字线)上绘制点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试找出在python中在数字行上绘制点的最佳方式。基本上试图做出类似下面的图片:

Trying to figure out what's the best way to graph a point on a number line in python. Essentially trying to make something similar to the image below:

我一直在尝试使用Matplotlib做到这一点,但似乎无法弄清楚。任何人知道一个包或任何东西,我可以使用?

I've been trying to use Matplotlib to do this but can't seem to figure it out. Anyone know of a package or anything out there I can use?

推荐答案

我不知道一个具体的包,您可以在Matplotlib中使用 hlines vlines 情节

I don't know of a specific package for this but you could do something like this in Matplotlib using hlines, vlines and plot.

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

这篇关于在Python中的直线(数字线)上绘制点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆