zsh2270365028的个人博客分享 http://blog.sciencenet.cn/u/zsh2270365028

博文

[转载]python Marker reference

已有 1477 次阅读 2022-1-21 11:39 |个人分类:Python|系统分类:科研笔记|文章来源:转载

refer to: https://matplotlib.org/stable/gallery/lines_bars_and_markers/marker_reference.html#sphx-glr-gallery-lines-bars-and-markers-marker-reference-py


Matplotlib supports multiple categories of markers which are selected using the marker parameter of plot commands:

For a list of all markers see also the matplotlib.markers documentation.

For example usages seeMarker examples.

import matplotlib.pyplot as pltfrom matplotlib.lines import Line2Dtext_style = dict(horizontalalignment='right', verticalalignment='center',
                  fontsize=12, fontfamily='monospace')marker_style = dict(linestyle=':', color='0.8', markersize=10,
                    markerfacecolor="tab:blue", markeredgecolor="tab:blue")def format_axes(ax):
    ax.margins(0.2)
    ax.set_axis_off()
    ax.invert_yaxis()def split_list(a_list):
    i_half = len(a_list) // 2
    return a_list[:i_half], a_list[i_half:]

Unfilled markers

Unfilled markers are single-colored.

fig, axs = plt.subplots(ncols=2)fig.suptitle('Un-filled markers', fontsize=14)# Filter out filled markers and marker settings that do nothing.unfilled_markers = [m for m, func in Line2D.markers.items()
                    if func != 'nothing' and m not in Line2D.filled_markers]for ax, markers in zip(axs, split_list(unfilled_markers)):
    for y, marker in enumerate(markers):
        ax.text(-0.5, y, repr(marker), **text_style)
        ax.plot([y] * 3, marker=marker, **marker_style)
    format_axes(ax)plt.show()

Un-filled markers

Filled markers

fig, axs = plt.subplots(ncols=2)fig.suptitle('Filled markers', fontsize=14)for ax, markers in zip(axs, split_list(Line2D.filled_markers)):
    for y, marker in enumerate(markers):
        ax.text(-0.5, y, repr(marker), **text_style)
        ax.plot([y] * 3, marker=marker, **marker_style)
    format_axes(ax)plt.show()
Filled markers

Marker fill styles

The edge color and fill color of filled markers can be specified separately. Additionally, the fillstyle can be configured to be unfilled, fully filled, or half-filled in various directions. The half-filled styles usemarkerfacecoloralt as secondary fill color.

fig, ax = plt.subplots()fig.suptitle('Marker fillstyle', fontsize=14)fig.subplots_adjust(left=0.4)filled_marker_style = dict(marker='o', linestyle=':', markersize=15,
                           color='darkgrey',
                           markerfacecolor='tab:blue',
                           markerfacecoloralt='lightsteelblue',
                           markeredgecolor='brown')for y, fill_style in enumerate(Line2D.fillStyles):
    ax.text(-0.5, y, repr(fill_style), **text_style)
    ax.plot([y] * 3, fillstyle=fill_style, **filled_marker_style)format_axes(ax)plt.show()
Marker fillstyle

Markers created from TeX symbols

Use MathText, to use custom marker symbols, like e.g. "$\u266B$". For an overview over the STIX font symbols refer to the STIX font table. Also see the STIX Fonts.

fig, ax = plt.subplots()fig.suptitle('Mathtext markers', fontsize=14)fig.subplots_adjust(left=0.4)marker_style.update(markeredgecolor="none", markersize=15)markers = ["$1$", r"$\frac{1}{2}$", "$f$", "$\u266B$", r"$\mathcal{A}$"]for y, marker in enumerate(markers):
    # Escape dollars so that the text is written "as is", not as mathtext.
    ax.text(-0.5, y, repr(marker).replace("$", r"\$"), **text_style)
    ax.plot([y] * 3, marker=marker, **marker_style)format_axes(ax)plt.show()
Mathtext markers

Markers created from Paths

Any Path can be used as a marker. The following example shows two simple paths star and circle, and a more elaborate path of a circle with a cut-out star.

import matplotlib.path as mpathimport numpy as npstar = mpath.Path.unit_regular_star(6)circle = mpath.Path.unit_circle()# concatenate the circle with an internal cutout of the starcut_star = mpath.Path(
    vertices=np.concatenate([circle.vertices, star.vertices[::-1, ...]]),
    codes=np.concatenate([circle.codes, star.codes]))fig, ax = plt.subplots()fig.suptitle('Path markers', fontsize=14)fig.subplots_adjust(left=0.4)markers = {'star': star, 'circle': circle, 'cut_star': cut_star}for y, (name, marker) in enumerate(markers.items()):
    ax.text(-0.5, y, name, **text_style)
    ax.plot([y] * 3, marker=marker, **marker_style)format_axes(ax)plt.show()
Path markers

Total running time of the script: ( 0 minutes 1.917 seconds)

Download Python source code: marker_reference.py

Download Jupyter notebook: marker_reference.ipynb

Keywords: matplotlib code example, codex, python plot, pyplot




https://blog.sciencenet.cn/blog-587102-1322005.html

上一篇:[转载]python Bayesian Methods for Hackers style sheet-alpha=0.8
下一篇:[转载]Py-Matplotlib: Plot 如何生成多个子图,以及它们如何共享坐标轴?或者共轴?
收藏 IP: 119.78.226.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-11-20 00:31

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部