Issue
I am trying to figure out if there is anything built into pyplot that will change the color of my line depending on whether or not the data is negative or positive. For example, if it is negative I'd like the line to be red and if it's positive I'd like the line to be a different color, say black.
Is there anything in the library that lets me do this? One thing I've thought of is to split the data into two sets of positive and negative and plotting them separately but I'm wondering if there is a better way.
Solution
I would just make two datasets and setting the right masks. By using that approach i wont have lines between different positive parts.
import matplotlib.pyplot as plt
import numpy as np
signal = 1.2*np.sin(np.linspace(0, 30, 2000))
pos_signal = signal.copy()
neg_signal = signal.copy()
pos_signal[pos_signal <= 0] = np.nan
neg_signal[neg_signal > 0] = np.nan
#plotting
plt.style.use('fivethirtyeight')
plt.plot(pos_signal, color='r')
plt.plot(neg_signal, color='b')
plt.savefig('pos_neg.png', dpi=200)
plt.show()
Answered By - tillsten
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.