user1850133
user1850133

Reputation: 2993

matplotlib custom markers

I would like markers such as empty squares with +, or x, or ., or anything inside with adjustable thickness; In fact the ones like in Origin. It seems that it needs customisation.

code example:

import numpy as np
import matplotlib.pyplot as plt

plt.plot(np.arange(10) ** 2,
         'k-',
         marker = 's',
         mfc = 'none',
         lw = 2,
         mew = 2,
         ms = 20)
plt.show()

Upvotes: 3

Views: 6617

Answers (2)

tom10
tom10

Reputation: 69242

Using text, you can use any character available in your fonts. You need to iterate through them yourself though, and I don't think that you can get continuous control over their linewidth (though, of course, you can select 'bold', etc, if available).

enter image description here

from numpy import *
import matplotlib.pyplot as plt

symbols = [u'\u2B21', u'\u263A', u'\u29C6', u'\u2B14', u'\u2B1A', u'\u25A6', u'\u229E', u'\u22A0', u'\u22A1', u'\u20DF']

x = arange(10.)
y = arange(10.)

plt.figure()
for i, symbol in enumerate(symbols):
    y2 = y + 4*i
    plt.plot(x, y2, 'g')
    for x0, y0 in zip(x, y2):
        plt.text(x0, y0, symbol, fontname='STIXGeneral', size=30, va='center', ha='center', clip_on=True)

plt.show()

You can also use plot directly, though the rendering doesn't look quite as good and you don't have quite as much control over the characters.

plt.figure()
for i, symbol in enumerate(symbols):
    y2 = y + 4*i
    plt.plot(x, y2, 'g')
    marker = "$%s$" % symbol
    plt.plot(x, y2, 'k', marker=marker, markersize=30)

enter image description here

Upvotes: 6

daedalus
daedalus

Reputation: 10923

Is this what you want?

Custom markers by overplotting

I did this by overplotting thus:

import numpy as np
import matplotlib.pyplot as plt

plt.plot(np.arange(10) ** 2,
         'k-',
         marker = 's',
         mfc = 'none',
         lw = 2,
         mew = 2,
         ms = 20)

plt.plot(np.arange(10) ** 2 + 20,
         'k-',
         marker = '+',
         mfc = 'none',
         lw = 2,
         mew = 2,
         ms = 20)
plt.plot(np.arange(10) ** 2 + 20,
         'k-',
         marker = 's',
         mfc = 'none',
         lw = 2,
         mew = 2,
         ms = 20)
plt.show()

Upvotes: 3

Related Questions