python-3.x为每个点绘制一条水平和垂直线(散点图)

时间:2018-05-01 10:25:13

标签: python python-3.x matplotlib drawing

我正在使用python-3.x绘制使用(matplotlib),我正在尝试根据我拥有的值绘制每个点(散点图)的水平和垂直线:

import matplotlib.pyplot as plt
x = [-0.9,  0.5  ,  -2.5,  3  , -1.5 ]
y = [2.9 ,  -1.5 ,  1   ,  1  , -2.4 ]
v = [50  ,  33   ,  21  ,  18 , 5    ]

fig = plt.figure(figsize=(10, 8))
plt.scatter(x,y, s=40 ,marker='o', c='black')
plt.grid()
plt.show()

问题我无法找到如何为每个点绘制水平和垂直线的任何答案,以及如何显示该线的y轴和x轴的值,如例子(附图)。

非常感谢任何建议。

我需要实现的完美范例: enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用次要刻度来创建网格和相应的刻度标记。

import matplotlib
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(25)

x,y = np.random.randn(2,7).round(1)

fig, ax = plt.subplots()
ax.scatter(x,y, c="crimson")


ax.set_xticks(x, minor=True)
ax.set_yticks(y, minor=True)
ax.xaxis.set_minor_formatter(matplotlib.ticker.ScalarFormatter())
ax.yaxis.set_minor_formatter(matplotlib.ticker.ScalarFormatter())
ax.tick_params(which="minor", labelbottom=True, labelleft=True, 
               labelcolor="crimson", pad=15)

ax.grid(which='minor', color="navy")

plt.show()

enter image description here

相关问题