sciapp / gr

GR framework: a graphics library for visualisation applications
Other
329 stars 54 forks source link

feature request: staircase #65

Closed nilsbecker closed 5 years ago

nilsbecker commented 5 years ago

gr.polyline draws a linear interpolation from point to point; gr.spline draws a cubic spline. sometimes (for histograms etc) it's nice to have a line that does a 'constant interpolation', i.e. looks like a staircase up or down. the function would need an optional argument that specifies if the interpolation is to the previous y value, the next y value, or a nearest-neighbor rule.

nilsbecker commented 5 years ago

(one could also think of adding quadratic splines, so that all interpolation orders 0-3 are covered. not sure the quadratic interpolation is very important)

nilsbecker commented 5 years ago

for reference:

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.step.html

one might argue that this is more high-level than the bare gr plotting functions? i still think it might be useful also to have available

FlorianRhiem commented 5 years ago

Yes, this is probably something for mlab and jlgr instead of gr itself.

Here's an example of a plot like this for the where=mid setting in matplotlib.pyplot.step with gr, other where settings require less manipulation of the input data and also boil down to a polyline:

import numpy as np
import gr

x = np.linspace(-2 * np.pi, 2 * np.pi, 30)
y = np.cos(x)

n = len(x)
x_steps = x[1:] - x[:-1]
x_step_boundaries = np.zeros(2 * n)
x_step_boundaries[0] = x[0] - (x[1] - x[0]) / 2
x_step_boundaries[1:-1][0::2] = (x[1:] + x[:-1]) / 2
x_step_boundaries[1:-1][1::2] = (x[1:] + x[:-1]) / 2
x_step_boundaries[-1] = x[-1] - (x[-1] - x[-2]) / 2
y_step_values = np.zeros(2 * n)
y_step_values[0::2] = y
y_step_values[1::2] = y

gr.setwindow(-2 * np.pi, 2 * np.pi, -1, 1)
gr.axes(0.5, 0.25, -2 * np.pi, -1, 4, 2, -0.0075)
gr.polyline(x_step_boundaries, y_step_values)
gr.updatews()

image

nilsbecker commented 5 years ago

i agree it's not overly hard to implement yourself on the basis of polyline but it would be nice to have this as a built-in function since it's needed every now and then and it's annoying to have to redo manually.

FlorianRhiem commented 5 years ago

mlab.step() will be part of the next release of python-gr. Here's a demo of the where parameter's effect: gks

nilsbecker commented 5 years ago

cool! thanks!