Hi,

I needed some help tweaking a multicolored line with matplotlib. I adopted

tacaswell's answer on StackOverflow at

http://stackoverflow.com/a/30125761/3566440

When I do the steps exactly like its explained in the answer, I am able to

replicate it. However, my case is a bit different. I have a dataframe with

timeseries as it's index, and 'perc99_99' as a set of Z-scores.

[image: Inline image 1]

I modified the SO answer to the best of my knowledge and this is what the

code looks like:

fig, ax = plt.subplots()

# Index of dataframe = timestamps

x = day_avg_zscore.index.values

# Z score of 99th percentiles

y = day_avg_zscore['perc99_99'].values

# Threshold at Z-score = 2, using helper function

lc = threshold_plot(ax, x, y, 2, 'k', 'r')

ax.axhline(2, color='k', ls='--')

lc.set_linewidth(3)

plt.show()

I left the helper function intact:

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.collections import LineCollection

from matplotlib.colors import ListedColormap, BoundaryNorm

def threshold_plot(ax, x, y, threshv, color, overcolor):

"""

Helper function to plot points above a threshold in a different color

Parameters

## ···

----------

ax : Axes

Axes to plot to

x, y : array

The x and y values

threshv : float

Plot using overcolor above this value

color : color

The color to use for the lower values

overcolor: color

The color to use for values over threshv

"""

# Create a colormap for red, green and blue and a norm to color

# f' < -0.5 red, f' > 0.5 blue, and the rest green

cmap = ListedColormap([color, overcolor])

norm = BoundaryNorm([np.min(y), threshv, np.max(y)], cmap.N)

# Create a set of line segments so that we can color them individually

# This creates the points as a N x 1 x 2 array so that we can stack points

# together easily to get the segments. The segments array for line

collection

# needs to be numlines x points per line x 2 (x and y)

points = np.array([x, y]).T.reshape(-1, 1, 2)

segments = np.concatenate([points[:-1], points[1:]], axis=1)

# Create the line collection object, setting the colormapping parameters.

# Have to set the actual values used for colormapping separately.

lc = LineCollection(segments, cmap=cmap, norm=norm)

lc.set_array(y)

ax.add_collection(lc)

ax.set_xlim(np.min(x), np.max(x))

ax.set_ylim(np.min(y)*1.1, np.max(y)*1.1)

return lc

Here's what I get as an output:

[image: Inline image 2]

What am I missing?

I'm using Anaconda Python2.7 and Jupyter Notebook.

Thank you!

- Deep

--

Sent from my phone. Please forgive typos and other forms of brevity.

-------------- next part --------------

An HTML attachment was scrubbed...

URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170202/37aa11bb/attachment-0001.html>

-------------- next part --------------

A non-text attachment was scrubbed...

Name: image.png

Type: image/png

Size: 44013 bytes

Desc: not available

URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170202/37aa11bb/attachment-0002.png>

-------------- next part --------------

A non-text attachment was scrubbed...

Name: image.png

Type: image/png

Size: 14568 bytes

Desc: not available

URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170202/37aa11bb/attachment-0003.png>