如何使用pylab在给定的y值处绘制一维数据 [英] How to plot 1-d data at given y-value with pylab

查看:40
本文介绍了如何使用pylab在给定的y值处绘制一维数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想沿着水平轴绘制一维数组中的数据点,如下图所示:

I want to plot the data points that are in a 1-D array just along the horizontal axis [edit: at a given y-value], like in this plot:

如何使用pylab做到这一点?

How can I do this with pylab?

推荐答案

Staven 已经编辑了他的帖子,包括如何沿 y 值 1 绘制值,但他使用的是 Python 列表.

Staven already edited his post to include how to plot the values along y-value 1, but he was using Python lists.

一个应该更快的变体(虽然我没有测量它)只使用 numpy 数组:

A variant that should be faster (although I did not measure it) only uses numpy arrays:

import numpy as np
import matplotlib.pyplot as pp
val = 0. # this is the value where you want the data to appear on the y-axis.
ar = np.arange(10) # just as an example array
pp.plot(ar, np.zeros_like(ar) + val, 'x')
pp.show()

作为一个很好用的函数,它通过 kwargs 提供所有常见的 matplotlib 改进,这将是:

As a nice-to-use function that offers all usual matplotlib refinements via kwargs this would be:

def plot_at_y(arr, val, **kwargs):
    pp.plot(arr, np.zeros_like(arr) + val, 'x', **kwargs)
    pp.show()

这篇关于如何使用pylab在给定的y值处绘制一维数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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