matplotlib作图的marker,color和linestyle

2020年9月11日 / 48次阅读 / Last Modified 2020年9月11日
Matplotlib

用matplotlib画好看的图,其实就是要用好它提供的API接口,没啥特别的。本文总结一下marker,color和linestyle这几个参数。

import numpy as np
import matplotlib.pyplot as plt

x1 = np.random.randn(100)
x2 = np.random.randn(100)
x3 = np.random.randn(100)
x4 = np.random.randn(100)

fig = plt.figure()
fig.suptitle('different ')
fig.subplots_adjust(hspace=0.4)

ax221 = fig.add_subplot(221)  
ax221.set_title('01')
ax221.plot(x1,color='red',marker='*',linestyle='-')
ax222 = fig.add_subplot(222)
ax222.set_title('02')
ax222.plot(x2,color='green',marker='p',linestyle=':')
ax223 = fig.add_subplot(223)
ax223.set_title('03')
ax223.plot(x3,color='purple',marker='s',linestyle='--')
ax224 = fig.add_subplot(224)   
ax224.set_title('04')
ax224.plot(x4)

plt.show()

效果如下:

matplotlib画出的不同的线条
matplotlib画出的不同的线条

其实就是marker,color,linestyle这几个参数,help中的信息如下:

**Markers**

=============    ===============================
character        description
=============    ===============================
``'.'``          point marker
``','``          pixel marker
``'o'``          circle marker
``'v'``          triangle_down marker
``'^'``          triangle_up marker
``'<'``          triangle_left marker
``'>'``          triangle_right marker
``'1'``          tri_down marker
``'2'``          tri_up marker
``'3'``          tri_left marker
``'4'``          tri_right marker
``'s'``          square marker
``'p'``          pentagon marker
``'*'``          star marker
``'h'``          hexagon1 marker
``'H'``          hexagon2 marker
``'+'``          plus marker
``'x'``          x marker
``'D'``          diamond marker
``'d'``          thin_diamond marker
``'|'``          vline marker
``'_'``          hline marker
=============    ===============================

**Line Styles**

=============    ===============================
character        description
=============    ===============================
``'-'``          solid line style
``'--'``         dashed line style
``'-.'``         dash-dot line style
``':'``          dotted line style
=============    ===============================

Example format strings::

    'b'    # blue markers with default shape
    'or'   # red circles
    '-g'   # green solid line
    '--'   # dashed line with default color
    '^k:'  # black triangle_up markers connected by a dotted line

**Colors**

The supported color abbreviations are the single letter codes

=============    ===============================
character        color
=============    ===============================
``'b'``          blue
``'g'``          green
``'r'``          red
``'c'``          cyan
``'m'``          magenta
``'y'``          yellow
``'k'``          black
``'w'``          white
=============    ===============================

and the ``'CN'`` colors that index into the default property cycle.

If the color is the only part of the format string, you can
additionally use any  `matplotlib.colors` spec, e.g. full names
(``'green'``) or hex strings (``'#008000'``).

-- EOF --

本文链接:https://www.pynote.net/archives/2471

留言区

《matplotlib作图的marker,color和linestyle》有1条留言

电子邮件地址不会被公开。 必填项已用*标注

  • 麦新杰

    还有个linewidth参数,也经常使用。 [回复]


前一篇:
后一篇:

More


©Copyright 麦新杰 Since 2019 Python笔记

go to top