python matplotlib 绘图线条类型和颜色选择

 

复制代码
import numpy as np
import matplotlib.pyplot as plt

linestyle_str = [
     ('solid', 'solid'),      # Same as (0, ()) or '-'
     ('dotted', 'dotted'),    # Same as (0, (1, 1)) or '.'
     ('dashed', 'dashed'),    # Same as '--'
     ('dashdot', 'dashdot')]  # Same as '-.'

linestyle_tuple = [
     ('loosely dotted',        (0, (1, 10))),
     ('dotted',                (0, (1, 1))),
     ('densely dotted',        (0, (1, 1))),

     ('loosely dashed',        (0, (5, 10))),
     ('dashed',                (0, (5, 5))),
     ('densely dashed',        (0, (5, 1))),

     ('loosely dashdotted',    (0, (3, 10, 1, 10))),
     ('dashdotted',            (0, (3, 5, 1, 5))),
     ('densely dashdotted',    (0, (3, 1, 1, 1))),

     ('dashdotdotted',         (0, (3, 5, 1, 5, 1, 5))),
     ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))),
     ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))]


def plot_linestyles(ax, linestyles):
    X, Y = np.linspace(0, 100, 10), np.zeros(10)
    yticklabels = []

    for i, (name, linestyle) in enumerate(linestyles):
        ax.plot(X, Y+i, linestyle=linestyle, linewidth=1.5, color='black')
        yticklabels.append(name)

    ax.set(xticks=[], ylim=(-0.5, len(linestyles)-0.5),
           yticks=np.arange(len(linestyles)), yticklabels=yticklabels)

    # For each line style, add a text annotation with a small offset from
    # the reference point (0 in Axes coords, y tick value in Data coords).
    for i, (name, linestyle) in enumerate(linestyles):
        ax.annotate(repr(linestyle),
                    xy=(0.0, i), xycoords=ax.get_yaxis_transform(),
                    xytext=(-6, -12), textcoords='offset points',
                    color="blue", fontsize=8, ha="right", family="monospace")


fig, (ax0, ax1) = plt.subplots(2, 1, gridspec_kw={'height_ratios': [1, 3]},
                               figsize=(10, 8))

plot_linestyles(ax0, linestyle_str[::-1])
plot_linestyles(ax1, linestyle_tuple[::-1])

plt.tight_layout()
plt.show()
复制代码

 

 

 

 

 

REF

https://matplotlib.org/3.1.0/gallery/lines_bars_and_markers/linestyles.html

The following format string characters are accepted to control the line style or marker:

characterdescription
'-' solid line style
'--' dashed line style
'-.' dash-dot line style
':' dotted line style
'.' 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

The following color abbreviations are supported:

charactercolor
‘b’ blue
‘g’ green
‘r’ red
‘c’ cyan
‘m’ magenta
‘y’ yellow
‘k’ black
‘w’ white

 

 

REF

good demos

https://matplotlib.org/2.1.1/api/_as_gen/matplotlib.pyplot.plot.html

All possible markers are defined here:

markersymboldescription
"." m00 point
"," m01 pixel
"o" m02 circle
"v" m03 triangle_down
"^" m04 triangle_up
"<" m05 triangle_left
">" m06 triangle_right
"1" m07 tri_down
"2" m08 tri_up
"3" m09 tri_left
"4" m10 tri_right
"8" m11 octagon
"s" m12 square
"p" m13 pentagon
"P" m23 plus (filled)
"*" m14 star
"h" m15 hexagon1
"H" m16 hexagon2
"+" m17 plus
"x" m18 x
"X" m24 x (filled)
"D" m19 diamond
"d" m20 thin_diamond
"|" m21 vline
"_" m22 hline
0 (TICKLEFT) m25 tickleft
1 (TICKRIGHT) m26 tickright
2 (TICKUP) m27 tickup
3 (TICKDOWN) m28 tickdown
4 (CARETLEFT) m29 caretleft
5 (CARETRIGHT) m30 caretright
6 (CARETUP) m31 caretup
7 (CARETDOWN) m32 caretdown
8 (CARETLEFTBASE) m33 caretleft (centered at base)
9 (CARETRIGHTBASE) m34 caretright (centered at base)
10 (CARETUPBASE) m35 caretup (centered at base)
11 (CARETDOWNBASE) m36 caretdown (centered at base)
"None"" or ""   nothing
'$...$' m37 Render the string using mathtext. E.g "$f$" for marker showing the letter f.
verts   A list of (x, y) pairs used for Path vertices. The center of the marker is located at (0,0) and the size is normalized, such that the created path is encapsulated inside the unit cell.
path   Path instance.
(numsides, style, angle)  

The marker can also be a tuple (numsides, style, angle), which will create a custom, regular symbol.

numsides:
the number of sides
style:

the style of the regular symbol:

  • 0: a regular polygon
  • 1: a star-like symbol
  • 2: an asterisk
  • 3: a circle (numsides and angle is ignored); deprecated.
angle:
the angle of rotation of the symbol

 

REF

https://matplotlib.org/3.1.0/api/markers_api.html

 

 

 

 

 

 

REF

https://matplotlib.org/stable/tutorials/colors/colors.html

https://matplotlib.org/stable/gallery/color/named_colors.html

 

posted @   emanlee  阅读(1600)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
历史上的今天:
2020-03-02 使用python内置2to3工具将python2代码转换为python3代码
2020-03-02 user installations are disabled via policy on the machine.
2020-03-02 python执行安装第三方依赖numpy失败:error: Unable to find vcvarsall.bat
2020-03-02 为python2.7安装包numpy RuntimeError: Python version >= 3.5 required.
2020-03-02 为python2.7安装包 UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position
2020-03-02 怎样让多张图片双面打印按顺序打印?
2009-03-02 GridView中的更新按钮不能触发RowUpdating事件
点击右上角即可分享
微信分享提示