Search code examples
pythonmatplotlibplotscatter-plot

How to change individual markers when plotting with matplotlib


I have the code below and am trying to figure out how to shape of markers in a matplotlib array. Specifically, the first marker point in the array should be a circle, and the second marker point in the array should be a square.

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

Solution

  • 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