Hug*_*ugh 5 python plot matplotlib
编辑:这个问题不是重复的,我不想绘制数字而不是点,我想在我的点旁边绘制数字。
我正在使用 matplotlib 绘制一个图。需要绘制三个点 [[3,9],[4,8],[5,4]]
我可以轻松地用它们制作散点图
import matplotlib.pyplot as plt
allPoints = [[3,9],[4,8],[5,4]]
f, diagram = plt.subplots(1)
for i in range(3):
xPoint = allPoints[i][0]
yPoint = allPoints[i][1]
diagram.plot(xPoint, yPoint, 'bo')
Run Code Online (Sandbox Code Playgroud)
这产生了这个情节:
我想用数字 1,2,3 标记每个点。
基于这个SO 答案,我尝试使用 annotate 来标记每个点。
import matplotlib.pyplot as plt
allPoints = [[1,3,9],[2,4,8],[3,5,4]]
f, diagram = plt.subplots(1)
for i in range(3):
pointRefNumber = allPoints[i][0]
xPoint = allPoints[i][1]
yPoint = allPoints[i][2]
diagram.annotate(pointRefNumber, (xPoint, yPoint))
Run Code Online (Sandbox Code Playgroud)
这会产生一个空白图。我正在密切关注另一个答案,但它没有产生任何情节。我哪里做错了?
gle*_*oux 10
你可以这样做:
import matplotlib.pyplot as plt
points = [[3,9],[4,8],[5,4]]
for i in range(len(points)):
x = points[i][0]
y = points[i][1]
plt.plot(x, y, 'bo')
plt.text(x * (1 + 0.01), y * (1 + 0.01) , i, fontsize=12)
plt.xlim((0, 10))
plt.ylim((0, 10))
plt.show()
Run Code Online (Sandbox Code Playgroud)
我解决了我自己的问题。我需要绘制点然后对它们进行注释,注释没有内置的绘图功能。
import matplotlib.pyplot as plt
allPoints = [[1,3,9],[2,4,8],[3,5,4]]
f, diagram = plt.subplots(1)
for i in range(3):
pointRefNumber = allPoints[i][0]
xPoint = allPoints[i][1]
yPoint = allPoints[i][2]
diagram.plot(xPoint, yPoint, 'bo')
diagram.annotate(nodeRefNumber, (xPoint, yPoint), fontsize=12)
Run Code Online (Sandbox Code Playgroud)
编辑以添加 fontsize 选项,就像 Gregoux 的答案一样