1

I have custom data on X and Y. Actually i'm using plt.fill_between to have an area chart with fixed color. I would like to have gradient color instead of fixed color.

import matplotlib.pyplot as plt
import pylab
import numpy as np
import matplotlib

x1 = ['LUG','17','18','19','22','23','24','25','26','29','30','31','AGO','2']
z = [0,0.27,0.6,0.42,-0.48,-0.53,0.41,-0.61,0.48,-0.25,1.04,1.57,1.07,1.69]

plt.subplot(4, 1, 4)
plt.fill_between(x1, z, color='yellow', alpha=0.1)
plt.fill_between( x1, z, color="yellow", alpha=0.1)
plt.plot(x1, z, color="red", alpha=0.6)
plt.axhline(y=0, linewidth=1, color='pink')
plt.xlabel('time (day)')

#legend
plt.legend(['Equity'], loc='lower left')

plt.tight_layout()
plt.show()
  • [Here](https://stackoverflow.com/questions/29321835/is-it-possible-to-get-color-gradients-under-curve-in-matplotlib) is how to draw a gradient for fill_between. Did you try to use such answer? What is the problem? – ImportanceOfBeingErnest Aug 04 '19 at 12:01
  • Ok, maybe that example is ok for me but i would like to know how to use my X1 and Z data, can you help me? – Giacomo Tassi Aug 05 '19 at 08:37
  • I guess if you can show inside the question in how far those answers do not help, it might be easier for others to help here. – ImportanceOfBeingErnest Aug 05 '19 at 10:04
  • Since all x values are distinct, you can assume `list(range(len(x1)))` as x values for that purpose. – ImportanceOfBeingErnest Aug 05 '19 at 13:04
  • Ok, sorry, but my code attached here is working fine, i only need gradient color from 0-line to z-values, please. I guess it's a stupid question... – Giacomo Tassi Aug 05 '19 at 13:16
  • Yes, how to get gradient color is shown in the question I linked to. You said the problems with this is that you cannot use your x values with those answers, to which I replied that you could use `list(range(len(x1)))` instead. – ImportanceOfBeingErnest Aug 05 '19 at 13:21
  • Doesn't works... Instead to have x = np.linspace(0, 100, num) i just copied your suggest but i have bad error: line 56, in gradient_fill xmin, xmax, ymin, ymax = x.min(), x.max(), y.min(), y.max() AttributeError: 'list' object has no attribute 'min' – Giacomo Tassi Aug 05 '19 at 13:37
  • Well, if those solution require a numpy array, use a numpy array instead, `np.array(list(range(len(x1))))` – ImportanceOfBeingErnest Aug 05 '19 at 13:40
  • Like this? x = p.array(list(range(len(x1)))) In that case i have new error: line 15, in generate_data x = p.array(list(range(len(x1)))) NameError: name 'p' is not defined – Giacomo Tassi Aug 05 '19 at 14:06

0 Answers0