使用matplotlib打印时如何更改单个标记

2024-10-03 23:19:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的代码,正在尝试找出如何在matplotlib数组中塑造标记的形状。具体而言,阵列中的第一个标记点应为圆形,阵列中的第二个标记点应为正方形

import matplotlib.pyplot as plt
import numpy as np

plt.title('Map')

plt.gca().invert_yaxis()

RedLineNames = np.array(["Tub Gallery","Maul Gallery","Rocket Gallery","ClasseArt Gallery ","Wiseworlds Gallery"])
RedLineCoords = np.array([[3950, 4250],[1350,450],[3550, 3200],[2500, 2500],[400, 2750]])

# Set marker points
plt.plot(RedLineCoords[:,0],RedLineCoords[:,1],marker='^',markersize=10,color='red')

# Draw connecting red line
plt.plot(RedLineCoords[:,0],RedLineCoords[:,1], 'r-')

# Add plot names
for i in range(len(RedLineCoords)):
    plt.text(RedLineCoords[i,0],RedLineCoords[i,1]+20,RedLineNames[i])
    
plt.show()

Tags: 代码标记importplotmatplotlibasnpplt
1条回答
网友
1楼 · 发布于 2024-10-03 23:19:55
  • 使用单独的绘图调用设置标记
  • 有关标记和颜色格式字符串,请参见^{}底部附近的注释部分
import numpy as np
import matplotlib.pyplot as plt

RedLineNames = np.array(["Tub Gallery","Maul Gallery","Rocket Gallery","ClasseArt Gallery ","Wiseworlds Gallery"])
RLC = np.array([[3950, 4250], [1350,450], [3550, 3200], [2500, 2500], [400, 2750]])

# create the figure and axes; use the object oriented approach
fig, ax = plt.subplots(figsize=(8, 6))

# draw the line plot
ax.plot(RLC[:,0], RLC[:,1], color='red')

# set the first two markers separately (black circle and blue square)
ax.plot(RLC[0, 0], RLC[0, 1], 'ok', RLC[1, 0], RLC[1, 1], 'sb')

# set the rest of the markers (green triangle)
ax.plot(RLC[2:, 0], RLC[2:, 1], '^g')

# Add plot names
for i in range(len(RLC)):
    ax.text(RLC[i,0], RLC[i,1]+20, RedLineNames[i])
    
ax.set_title('Map')

ax.invert_yaxis()
    
plt.show()

enter image description here

相关问题 更多 >