dhaitz/mplcyberpunk

`add_glow_effects(gradient_fill=True)` treats step plot like a line plot

diceroll123 opened this issue ยท 5 comments

plt.style.use("cyberpunk")

ax = plt.gca()
ax.step(range(7), [0, 1, 2, 3, 2, 1, 0], marker="o")
mplcyberpunk.add_glow_effects(gradient_fill=True)
plt.show()

image

This doesn't seem like an easy one.

I've found a possible band-aid solution, but I'm not very happy with it, nor do I trust it very much. and to be fair I have not tested this extensively yet, just sharing findings!

Since I can't for the life of me find anywhere that might have the points that are plotted as lines, besides the x and y values we pass in, I figure just manipulating the points in the gradient filler before it gets to this line, will get the job done.

The algorithm for mutating the X and Y values to make a Polygon into a pre step plot is as follows:

x = x.repeat(2)
x = np.delete(x, -1)
y = y.repeat(2)
y = np.delete(y, 0)

And looks great!

image


Currently, I'm stumped on where in the code this could be generalized for all the different kinds of plots. ๐Ÿค” There's three kinds of step plots, for example. plt.step(where=['mid', 'pre', 'post'])

I think the following works best, as it is using the auto-generated matplolib path instead of the data, it works out-of-the-box with every kind of plot ( 3 steps and the normal lines)

        # xy = np.column_stack([x, y])
        # xy = np.vstack([[xmin, Ay], xy, [xmax, Ay], [xmin, Ay]])
        # clip_path = Polygon(xy, facecolor='none', edgecolor='none', closed=True)
        # ax.add_patch(clip_path)
        # im.set_clip_path(clip_path)

        path = line.get_path()
        extras = Path([[xmax,Ay],[xmin, Ay]], np.full(2, Path.MOVETO))
        extras.codes[:] = Path.LINETO
        path = path.make_compound_path(path, extras)
        im.set_clip_path(path, line._transform)

Here are the results:
test_gradient_step

Ahhh you found what I was looking for, awesome!

Thanks to @cedrichol, this should be implemented with #20 and published in v0.6.0. @diceroll123 feel free to check!

Works great.