Skip to content Skip to sidebar Skip to footer

Mapping A Plus Symbol With Matplotlib

I want to use mapplotlib to graph a plus symbol that looks like this: _ _| |_ |_ _| |_| I've been reading through the matplotlib docs but, frankly, I'm not even sure what

Solution 1:

  1. Draw your desired figure on to graph paper,
  2. write down the x,y values of the corners,
  3. put those values into a pair of lists, (one for x and one for y), in the same order,
  4. plot it.

For example:

>>>import matplotlib.pyplot as plt>>>fig, ax = plt.subplots()>>>y =[10, 20, 20, 30, 30, 40, 40, 30, 30, 20, 20, 10, 10]>>>x =[10, 10, 0, 0, 10, 10, 20, 20, 30, 30, 20, 20, 10]>>>line, = ax.plot(x, y, 'go-')>>>ax.grid()>>>ax.axis('equal')
(0.0, 30.0, 10.0, 40.0)
>>>plt.show()

Produces:enter image description here

Solution 2:

If you would have done a little search you should have found a few links how to create custom markers. The best I came up with to answer your question is to use a Path object as marker. Therefore you can create a function which creates the desired path (I was to lazy to write the cross so I took a simpler rectangle):

def getCustomMarker():
    verts = [(-1, -1), # left, bottom
             (-1, 1), # left, top
             (1, 1), # right, top
             (1, -1), # right, bottom
             (-1, -1)] # ignored

    codes = [matplotlib.path.Path.MOVETO,
             matplotlib.path.Path.LINETO,
             matplotlib.path.Path.LINETO,
             matplotlib.path.Path.LINETO,
             matplotlib.path.Path.CLOSEPOLY]

    path = matplotlib.path.Path(verts, codes)
    return path

You are now able to plot any data with the desired custom marker:

import matplotlib
import matplotlib.pyplotas plt
import numpy as np

x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

figure = plt.figure()
axes = figure.add_subplot(1, 1, 1)

axes.plot(x, y, marker=getCustomMarker(), markerfacecolor='none', markersize=3)

plt.show()

This enables you to plot any marker at any position you want it to be at the desired size.

Post a Comment for "Mapping A Plus Symbol With Matplotlib"