matplotlib 自定义标记 [英] matplotlib custom markers

查看:81
本文介绍了matplotlib 自定义标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要标记,例如带有+或x或.的空心正方形,或内部任何厚度可调的标记;事实上,就像 Origin 中的那些.好像需要定制.

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.

代码示例:

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

推荐答案

使用 text,您可以使用字体中可用的任何字符.不过,您需要自己遍历它们,而且我认为您无法持续控制它们的线宽(当然,如果可用,您可以选择粗体"等).

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

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

您也可以直接使用 plot,尽管渲染效果看起来不太好,而且您对字符的控制也不多.

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)

这篇关于matplotlib 自定义标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆