Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 646
user1850133 Avatar asked Sep 01 '26 08:09

user1850133


2 Answers

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

like image 161
tom10 Avatar answered Sep 03 '26 21:09

tom10


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()
like image 41
daedalus Avatar answered Sep 03 '26 21:09

daedalus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!