誰能告訴我如何使用 Python 創建這樣的影像?我確實畫了一條線,但我不知道如何更改 x 和 y 值。
輸入:

輸出:

from matplotlib import image
from matplotlib import pyplot as plt
# to read the image stored in the working directory
data = image.imread('Darwin_Maps.png')
# to draw a line from (200, 300) to (500, 100)
x = [200, 500]
y = [300, 100]
plt.plot(x, y, color="white", linewidth=3)
plt.imshow(data)
plt.show()
uj5u.com熱心網友回復:
您可以使用range在回圈中繪制線條:
from matplotlib import image
from matplotlib import pyplot as plt
data = image.imread('Darwin_Maps.png')
img_height, img_width, _ = data.shape
x_space = 120 #this is in pixel, you might want to change it depending on the scale of your map
x_space_km = 5
y_space = 120 #this is in pixel, you might want to change it depending on the scale of your map
y_space_km = 5
xlabel = x_space_km
for x in range(x_space, img_width, x_space):
plt.plot([x, x], [0, img_height], color="white", linewidth=3)
plt.text(x, 40, xlabel, color="red")
xlabel = x_space_km
ylabel = y_space_km
for y in range(x_space, img_height, y_space):
plt.plot([0, img_width], [y, y], color="white", linewidth=3)
plt.text(40, y, ylabel, color="red")
ylabel = y_space_km
plt.imshow(data)
plt.show()
輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/317817.html
標籤:Python matplotlib
