0

Suppose one has a plot like this, for which the peak is at (x,y) = (0,0.40). The distribution is plotted in blue. Is it possible to edit the color scheme of the distribution plot in such a way that the color is a gradient - the farther from x (or y or independently for both) the more the color changes - like this?

I've searched SO for help with this, but only found solutions in which line segments were different colors. But, I want the color transition to be smooth (like this but not 3-D) instead of rough, and I want the color to depend on its distance from a particular value rather than pre-determined "randomly". A different SO post did something similar (not quite what I want though), but could only do so as a scatter plot, which only works for changing colors based on x-value if the peak is at x=0 - I'd prefer it be generalized. As an example, the further from x=0 the redder the curve gets. Ideally, there's a way to do this with a matplotlib colormap.

  • It's not clear from the question whether you want to change the line color or the color below the curve. – ImportanceOfBeingErnest Apr 06 '17 at 07:45
  • If you're talking about the line color, you may start at the [matplotlib tutorial](http://matplotlib.org/examples/pylab_examples/multicolored_line.html) or [this question](http://stackoverflow.com/questions/13622909/matplotlib-how-to-colorize-a-large-number-of-line-segments-as-independent-gradi) of which this is probably a duplicate. – ImportanceOfBeingErnest Apr 06 '17 at 07:51
  • 1
    If it's about the fill below the curve, [this question](http://stackoverflow.com/questions/18215276/how-to-fill-rainbow-color-under-a-curve-in-python-matplotlib) already gives the answer. – ImportanceOfBeingErnest Apr 06 '17 at 07:54
  • For an xy- plot, I can use `plt.fill_between` and `where` to shade a region with a single color. While I am curious about how to apply a colormap for that purpose, that is a secondary concern. My primary goal is to change the line color. I'll edit the question accordingly. If the color changes as a function of distance from the x-coordinate of the peak, one can [compare like so](https://i.imgur.com/tlhdNJX.png). –  Apr 06 '17 at 07:55
  • The linked matplotlib tutorial shows how to do this for colored line segments but not a smooth color transition (unless one iterates a ridiculous amount of times). As for the linked SO response, the two lines plotted have blue and red at the same x-coordinates, so it appears arbitrary or defined as a function of the relative length of the curve (or list), which is not what I want. Though the fill below the curve (while not my specific question) was still helpful. –  Apr 06 '17 at 08:06
  • 1
    I think you have a misconception here. A 'gradient' must necesarily consist of many line segments. The smoother the gradient, the more segments you need. You will have to use a `Collection` for this to perform well, as detailed in the linked question and also the matplotlib tutorial. The technique can be applied to your case as well. Since it's not clear why you think this not to be the case, you will need to update your question showing the code that attempts it and ask specifically about the problem you have when implementing this technique. – ImportanceOfBeingErnest Apr 06 '17 at 08:19
  • Ah, that makes sense. The color gradient in photoshop is like a colormap in matplotlib in the sense that one does not need to define each line segment inbetween. Thanks for clearing that up. I was hoping for an alternative method that is more efficient. But based on other answers, I am under the impression that this is not yet a standard feature. –  Apr 06 '17 at 08:24
  • 1
    Possible duplicate of [Matplotlib: How to colorize a large number of line segments as independent gradients, efficiently](http://stackoverflow.com/questions/13622909/matplotlib-how-to-colorize-a-large-number-of-line-segments-as-independent-gradi) – ImportanceOfBeingErnest Apr 06 '17 at 08:39

0 Answers0