如何在给定的图上绘制垂直线

2024-12-23 08:43:00
admin
原创
137
摘要:问题描述:给定一个以时间表示的信号图,我如何绘制标记相应时间索引的线?具体来说,给定一个时间索引范围从 0 到 2.6(秒)的信号图,我想绘制垂直红线以指示列表的相应时间索引[0.22058956, 0.33088437, 2.20589566]。我该怎么做?解决方案 1:添加覆盖整个绘图窗口的垂直线的标准方...

问题描述:

给定一个以时间表示的信号图,我如何绘制标记相应时间索引的线?

具体来说,给定一个时间索引范围从 0 到 2.6(秒)的信号图,我想绘制垂直红线以指示列表的相应时间索引[0.22058956, 0.33088437, 2.20589566]。我该怎么做?


解决方案 1:

添加覆盖整个绘图窗口的垂直线的标准方法是,无需指定其实际高度plt.axvline

import matplotlib.pyplot as plt

plt.axvline(x=0.22058956)
plt.axvline(x=0.33088437)
plt.axvline(x=2.20589566)

或者

xcoords = [0.22058956, 0.33088437, 2.20589566]
for xc in xcoords:
    plt.axvline(x=xc)

您可以使用其他绘图命令中可用的许多关键字(例如color,,...)。您可以传入关键字参数linestyle,如果您愿意,还可以传入轴坐标(例如,将覆盖绘图的中间一半)。水平线( )和矩形( )有相应的函数。linewidth`yminymaxymin=0.25ymax=0.75axhline`axvspan

解决方案 2:

matplotlib.pyplot.vlines对阵matplotlib.pyplot.axvline

  • 这些方法适用于使用 seaborn 和 生成的图pandas.DataFrame.plot,两者都使用matplotlib

  • 不同之处vlines在于 接受 一个或多个位置x,而axvline允许一个位置。

    • 单一位置:x=37

    • 多个位置:x=[37, 38, 39]

  • vlinesyminymax作为 y 轴上的位置,而axvlineyminymax作为 y 轴范围的百分比。

    • 当传递多行到 时vlines,将 a 传递listyminymax

  • matplotlib.axes.Axes.vlines适用matplotlib.axes.Axes.axvline于面向对象的 API。

    • 如果您要绘制类似 的图形fig, ax = plt.subplots(),则分别将plt.vlines或替换plt.axvlineax.vlinesax.axvline

  • 请参阅此答案以了解带有的水平线.hlines

import numpy as np
import matplotlib.pyplot as plt

xs = np.linspace(1, 21, 200)

plt.figure(figsize=(10, 7))

# only one line may be specified; full height
plt.axvline(x=36, color='b', label='axvline - full height')

# only one line may be specified; ymin & ymax specified as a percentage of y-range
plt.axvline(x=36.25, ymin=0.05, ymax=0.95, color='b', label='axvline - % of full height')

# multiple lines all full height
plt.vlines(x=[37, 37.25, 37.5], ymin=0, ymax=len(xs), colors='purple', ls='--', lw=2, label='vline_multiple - full height')

# multiple lines with varying ymin and ymax
plt.vlines(x=[38, 38.25, 38.5], ymin=[0, 25, 75], ymax=[200, 175, 150], colors='teal', ls='--', lw=2, label='vline_multiple - partial height')

# single vline with full ymin and ymax
plt.vlines(x=39, ymin=0, ymax=len(xs), colors='green', ls=':', lw=2, label='vline_single - full height')

# single vline with specific ymin and ymax
plt.vlines(x=39.25, ymin=25, ymax=150, colors='green', ls=':', lw=2, label='vline_single - partial height')

# place the legend outside
plt.legend(bbox_to_anchor=(1.0, 1), loc='upper left')

plt.show()

在此处输入图片描述

Seaborn轴级图

import seaborn as sns

# sample data
fmri = sns.load_dataset("fmri")

# x index for max y values for stim and cue
c_max, s_max = fmri.pivot_table(index='timepoint', columns='event', values='signal', aggfunc='mean').idxmax()

# plot
g = sns.lineplot(data=fmri, x="timepoint", y="signal", hue="event")

# y min and max
ymin, ymax = g.get_ylim()

# vertical lines
g.vlines(x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2)

在此处输入图片描述

Seaborn图形级绘图

  • 必须对每个轴进行迭代。

import seaborn as sns

# sample data
fmri = sns.load_dataset("fmri")

# used to get the index values (x) for max y for each event in each region
fpt = fmri.pivot_table(index=['region', 'timepoint'], columns='event', values='signal', aggfunc='mean')

# plot
g = sns.relplot(data=fmri, x="timepoint", y="signal", col="region", hue="event", kind="line")

# iterate through the axes
for ax in g.axes.flat:
    # get y min and max
    ymin, ymax = ax.get_ylim()
    # extract the region from the title for use in selecting the index of fpt
    region = ax.get_title().split(' = ')[1]
    # get x values for max event
    c_max, s_max = fpt.loc[region].idxmax()
    # add vertical lines
    ax.vlines(x=[c_max, s_max], ymin=ymin, ymax=ymax, colors=['tab:orange', 'tab:blue'], ls='--', lw=2, alpha=0.5)
  • 因为'region = frontal'两个事件的最大值都发生在5

在此处输入图片描述

条形图

  • 条形图具有分类独立轴,因此刻度位置具有从零开始的索引,而不管轴刻度标签如何。

    • x根据条形索引而不是刻度标签进行选择。ax.get_xticklabels()将显示位置和标签。

import pandas as pd
import seaborn as sns

# load data
tips = sns.load_dataset('tips')

# histogram
ax = tips.plot(kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Vertical Line')
_ = ax.vlines(x=16.5, ymin=0, ymax=30, colors='r')

# barplot
ax = tips.loc[5:25, ['total_bill', 'tip']].plot(kind='bar', figsize=(15, 4), title='Barplot with Vertical Lines', rot=0)
_ = ax.vlines(x=[0, 17], ymin=0, ymax=45, colors='r')

在此处输入图片描述

直方图

  • 直方图有一个连续独立的轴。

import pandas as pd
import seaborn as sns

# load data
tips = sns.load_dataset('tips')

# histogram from pandas, pyplot, or seaborn 
ax = tips.plot(kind='hist', y='total_bill', bins=30, ec='k', title='Histogram with Vertical Line')
_ = ax.vlines(x=16.5, ymin=0, ymax=30, colors='r')

在此处输入图片描述

时间序列轴

  • 作为 x 轴的数据框中的日期必须是datetime dtype。如果列或索引不是正确的类型,则必须将其转换为pd.to_datetime

    • 如果使用日期数组或列表,请分别参考将 numpy 字符串数组转换为日期时间 或将日期时间列表转换为日期 python。

  • x`'2020-09-24'将接受像或 这样的日期datetime(2020, 9, 2)`。

import pandas_datareader as web  # conda or pip install this; not part of pandas
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime

# get test data; this data is downloaded with the Date column in the index as a datetime dtype
df = web.DataReader('^gspc', data_source='yahoo', start='2020-09-01', end='2020-09-28').iloc[:, :2]

# display(df.head(2))
                   High          Low
Date                                
2020-09-01  3528.030029  3494.600098
2020-09-02  3588.110107  3535.229980

# plot dataframe; the index is a datetime index
ax = df.plot(figsize=(9, 6), title='S&P 500', ylabel='Price')

# add vertical lines
ax.vlines(x=[datetime(2020, 9, 2), '2020-09-24'], ymin=3200, ymax=3600, color='r', label='test lines')

ax.legend(bbox_to_anchor=(1, 1), loc='upper left')
plt.show()

在此处输入图片描述

解决方案 3:

对于多行

xposition = [0.3, 0.4, 0.45]
for xc in xposition:
    plt.axvline(x=xc, color='k', linestyle='--')

解决方案 4:

要向某些垂直线添加legendand/or colors,请使用以下命令:

import matplotlib.pyplot as plt

# x coordinates for the lines
xcoords = [0.1, 0.3, 0.5]
# colors for the lines
colors = ['r','k','b']

for xc,c in zip(xcoords,colors):
    plt.axvline(x=xc, label='line at x = {}'.format(xc), c=c)

plt.legend()
plt.show()

结果

我的惊人情节 seralouk

解决方案 5:

像其他人建议的那样,循环调用 axvline 是可行的,但它可能不方便,因为

  1. 每一行都是一个单独的绘图对象,当有许多行时,这会导致运行非常慢。

  2. 当您创建图例时,每一行都有一个新条目,这可能不是您想要的。

相反,您可以使用以下便捷函数将所有线条创建为单个绘图对象:

import matplotlib.pyplot as plt
import numpy as np


def axhlines(ys, ax=None, lims=None, **plot_kwargs):
    """
    Draw horizontal lines across plot
    :param ys: A scalar, list, or 1D array of vertical offsets
    :param ax: The axis (or none to use gca)
    :param lims: Optionally the (xmin, xmax) of the lines
    :param plot_kwargs: Keyword arguments to be passed to plot
    :return: The plot object corresponding to the lines.
    """
    if ax is None:
        ax = plt.gca()
    ys = np.array((ys, ) if np.isscalar(ys) else ys, copy=False)
    if lims is None:
        lims = ax.get_xlim()
    y_points = np.repeat(ys[:, None], repeats=3, axis=1).flatten()
    x_points = np.repeat(np.array(lims + (np.nan, ))[None, :], repeats=len(ys), axis=0).flatten()
    plot = ax.plot(x_points, y_points, scalex = False, **plot_kwargs)
    return plot


def axvlines(xs, ax=None, lims=None, **plot_kwargs):
    """
    Draw vertical lines on plot
    :param xs: A scalar, list, or 1D array of horizontal offsets
    :param ax: The axis (or none to use gca)
    :param lims: Optionally the (ymin, ymax) of the lines
    :param plot_kwargs: Keyword arguments to be passed to plot
    :return: The plot object corresponding to the lines.
    """
    if ax is None:
        ax = plt.gca()
    xs = np.array((xs, ) if np.isscalar(xs) else xs, copy=False)
    if lims is None:
        lims = ax.get_ylim()
    x_points = np.repeat(xs[:, None], repeats=3, axis=1).flatten()
    y_points = np.repeat(np.array(lims + (np.nan, ))[None, :], repeats=len(xs), axis=0).flatten()
    plot = ax.plot(x_points, y_points, scaley = False, **plot_kwargs)
    return plot

解决方案 6:

除了上述答案中提供的plt.axvlineandplt.plot((x1, x2), (y1, y2)) 或之外,还可以使用 plt.plot([x1, x2], [y1, y2])

plt.vlines(x_pos, ymin=y1, ymax=y2)

在 到 处绘制一条垂直线,其中值和以绝对数据坐标x_pos表示。y1`y2y1y2`

相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   1565  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1354  
  信创国产芯片作为信息技术创新的核心领域,对于推动国家自主可控生态建设具有至关重要的意义。在全球科技竞争日益激烈的背景下,实现信息技术的自主可控,摆脱对国外技术的依赖,已成为保障国家信息安全和产业可持续发展的关键。国产芯片作为信创产业的基石,其发展水平直接影响着整个信创生态的构建与完善。通过不断提升国产芯片的技术实力、产...
国产信创系统   21  
  信创生态建设旨在实现信息技术领域的自主创新和安全可控,涵盖了从硬件到软件的全产业链。随着数字化转型的加速,信创生态建设的重要性日益凸显,它不仅关乎国家的信息安全,更是推动产业升级和经济高质量发展的关键力量。然而,在推进信创生态建设的过程中,面临着诸多复杂且严峻的挑战,需要深入剖析并寻找切实可行的解决方案。技术创新难题技...
信创操作系统   27  
  信创产业作为国家信息技术创新发展的重要领域,对于保障国家信息安全、推动产业升级具有关键意义。而国产芯片作为信创产业的核心基石,其研发进展备受关注。在信创国产芯片的研发征程中,面临着诸多复杂且艰巨的难点,这些难点犹如一道道关卡,阻碍着国产芯片的快速发展。然而,科研人员和相关企业并未退缩,积极探索并提出了一系列切实可行的解...
国产化替代产品目录   28  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用