0

I am pretty much a beginner in using python's matplotlib library. I have ten plots to be drawn in the same figure, using matplotlib.pyplot.plot assigns. I would like to pick the colors from a ranger "shorter" than the default one, for example "around the red" (like from reddish purple to orange). Is it possible? How can I do it in python?

I also found this https://matplotlib.org/examples/color/colormaps_reference.html, but it seems quite useless to me.

Thanks for answering

EDIT: to better clarify what I need, I am looking for a way to communicate to the "plt.plot" function that I want it to iterate over a different set of colors

wetrust
  • 57
  • 7
  • You could check out: https://matplotlib.org/3.1.1/tutorials/colors/colormap-manipulation.html – Pete Apr 09 '20 at 20:50

1 Answers1

0

If you want to create a color ramp you can do the following. Using https://matplotlib.org/3.2.1/tutorials/colors/colormap-manipulation.html as a reference:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

def plot_examples(colormaps):
    """
    Helper function to plot data with associated colormap.
    """
    np.random.seed(19680801)
    data = np.random.randn(30, 30)
    n = len(colormaps)
    fig, axs = plt.subplots(1, n, figsize=(n * 2 + 2, 3),
                            constrained_layout=True, squeeze=False)
    for [ax, cmap] in zip(axs.flat, colormaps):
        psm = ax.pcolormesh(data, cmap=cmap, rasterized=True, vmin=-4, vmax=4)
        fig.colorbar(psm, ax=ax)
    plt.show()

colors = ["purple", "red"]
cmap1 = LinearSegmentedColormap.from_list("mycmap", colors)
plot_examples([cmap1])

enter image description here

You can also use the colormap to get values for a normal plot:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

# Creating the colar map
colors = ["purple", "red"]
cmap1 = LinearSegmentedColormap.from_list("mycmap", colors)

# Data used in plot
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

plt.plot(t, s, color=cmap1(0.1))
plt.show()

Here you can change the 0.1 in the second to last line to choose where on the colormap you want to query (0-255).

enter image description here

Pete
  • 569
  • 3
  • 9
  • Thanks, but how can I apply this to a plt.plot ? – wetrust Apr 09 '20 at 21:07
  • Think I might have misunderstood want you wanted. How do you want the lines to be colored? You want each line to have a unique color or you want it to change over the graph. If you just want to a specific color you can specify it in the parameter to plot: `plt.plot(t, s, color="red")`. – Pete Apr 09 '20 at 21:29
  • I would like, for each of my 10 curves, to have a different red-like color. Say one color, of the ones emerging from your answer, for each curve. I know I can specify color="r", but I would like this to be automatic, and, moreover, to explore different shades of red (it's not a reference to the novel, lol) – wetrust Apr 09 '20 at 21:31
  • I think it surely helps. Just a final question: this means that I have to manually set an iteration over cmap1, right? If it's not possible to automatically set the cmap1 colors as "default", the question is over. – wetrust Apr 09 '20 at 21:47
  • Check out https://stackoverflow.com/questions/9397944/how-to-set-the-default-color-cycle-for-all-subplots-with-matplotlib – Pete Apr 09 '20 at 21:51