matplotlib:为线上的单个点设置标记 [英] matplotlib: Set markers for individual points on a line

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

问题描述

我已使用matplotlib在图形上绘制线条.现在,我想为直线上的各个点设置样式,特别是标记.我该怎么做?

I have used matplotlib to plot lines on a figure. Now I would now like to set the style, specifically the marker, for individual points on the line. How do I do this?

为了澄清我的问题,得到了回答,我希望能够为一行中的单个标记设置样式,而不是为该行上的每个标记设置样式.

to clarify my question, which was answered, I want to be able to set the style for individual markers on a line, not every marker on said line.

推荐答案

在调用plot时指定关键字args linestyle和/或marker.

Specify the keyword args linestyle and/or marker in your call to plot.

例如,使用虚线和蓝色圆圈标记:

For example, using a dashed line and blue circle markers:

plt.plot(range(10), linestyle='--', marker='o', color='b')

同一件事的快捷方式调用:

A shortcut call for the same thing:

plt.plot(range(10), '--bo')

以下是可能的线条和标记样式的列表:

Here is a list of the possible line and marker styles:

================    ===============================
character           description
================    ===============================
   -                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
================    ===============================


,其中包含根据注释要求标记任意点子集的示例:


edit: with an example of marking an arbitrary subset of points, as requested in the comments:

import numpy as np
import matplotlib.pyplot as plt

xs = np.linspace(-np.pi, np.pi, 30)
ys = np.sin(xs)
markers_on = [12, 17, 18, 19]
plt.plot(xs, ys, '-gD', markevery=markers_on)
plt.show()

由于markevery kwarg的最后一个示例是可能的. >此功能分支.如果您坚持使用较旧的matplotlib版本,则仍然可以通过在散点图上覆盖散点图来获得结果.有关更多详细信息,请参见编辑历史记录.

This last example using the markevery kwarg is possible in since 1.4+, due to the merge of this feature branch. If you are stuck on an older version of matplotlib, you can still achieve the result by overlaying a scatterplot on the line plot. See the edit history for more details.

这篇关于matplotlib:为线上的单个点设置标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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