0

enter image description here

So I want to change the color of the blue vlines(matplotlib) in the above plot. First I want to make the negative(< 0) values different color and take their absolute so that only amplitude is visible but they will be a different color than the negative ones. Positive values could remain unchanged.

minimum reproducible code as below:

import numpy as np
import random
import matplotlib.pyplot as plt
peakmzs = np.array([random.uniform(506, 2000) for i in range(2080)])
peakmzs = peakmzs[peakmzs.argsort()[::1]]

spec = np.zeros_like(peakmzs)
b = np.where((peakmzs > 1500) & (peakmzs < 1540))[0]
spec[b] = [random.uniform(0, 0.002) for i in range(len(b))]

b = np.where((peakmzs > 700) & (peakmzs < 820))[0]
spec[b] = [random.uniform(0, 0.05) for i in range(len(b))]

spec[300:302] = 0.07

b = np.where((peakmzs > 600) & (peakmzs < 650))[0]
spec[b] = [random.uniform(0, 0.03) for i in range(len(b))]

plt.vlines(peakmzs, spec, ymax=spec.max())
plt.show()


shp_values = np.zeros_like(peakmzs)
b = np.where((peakmzs > 1500) & (peakmzs < 1540))[0]
b_ = np.random.randint(1500, 1540, 10)
# print(b_)
shp_values[b] = [random.uniform(-0.003, 0.002) for i in range(len(b))]
shp_values[b_] = 0 

b = np.where((peakmzs > 700) & (peakmzs < 820))[0]
shp_values[b] = [random.uniform(-0.004, 0.002) for i in range(len(b))]
b_ = np.random.randint(700, 820, 70)
shp_values[b_] = 0
# [random.uniform(-0.005, 0.003) for i in range(len(peakmzs))]
plt.plot(shp_values)
banikr
  • 63
  • 1
  • 9
  • I haven't tried the list color method. Added MRE. Please take a look. – banikr Dec 16 '22 at 19:53
  • 1
    Indeed, an MRE is needed here. Maybe something like `spec = np.array(spec); colors = np.where(spec< 0, 'red', 'blue'); plt.vlines(peakmzs, spec, ymax=spec.max(), colors=colors)`. – JohanC Dec 16 '22 at 21:04
  • Did you mean `shp_values` instead of `spec`? It worked! Also, how do I add shades of blue and red based on the amplitudes of the `shp_values`. For example, if the value is too negative it is deep red, and vice versa. – banikr Dec 20 '22 at 17:32

1 Answers1

0

Based on the suggestion from @JohanC,

demo_shp = np.array(shapvalues[0][19])
colors = np.where(demo_shp < 0, 'cyan', 'pink')
plt.vlines(peakmzs, ymin=[0], ymax=demo_shp, colors=colors)
plt.show()
banikr
  • 63
  • 1
  • 9