ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 19 Mar 2021 16:06:14 +0100Automatically adding colors to multiple list_plot graphicshttps://ask.sagemath.org/question/56240/automatically-adding-colors-to-multiple-list_plot-graphics/Is there a way to *automatically* color different plots?
For example, I'm trying to plot complex points using `list_plot`, and the points are all stored in a list of lists, each of which I would like to be a different color. So, in order to plot these points, I have
list=[list1,list2,...]
plot = sum(list_plot(i) for i in list)
I'm wondering if there's any (somewhat simple) way to add different colors corresponding to each list in an iterable fashion, rather than typing out
plot=list_plot([i for i in list1], rgbcolor='red')+list_plot([i for i in list2], rgbcolor='orange')+...Fri, 19 Mar 2021 01:29:42 +0100https://ask.sagemath.org/question/56240/automatically-adding-colors-to-multiple-list_plot-graphics/Answer by slelievre for <p>Is there a way to <em>automatically</em> color different plots?</p>
<p>For example, I'm trying to plot complex points using <code>list_plot</code>, and the points are all stored in a list of lists, each of which I would like to be a different color. So, in order to plot these points, I have </p>
<pre><code>list=[list1,list2,...]
plot = sum(list_plot(i) for i in list)
</code></pre>
<p>I'm wondering if there's any (somewhat simple) way to add different colors corresponding to each list in an iterable fashion, rather than typing out </p>
<pre><code>plot=list_plot([i for i in list1], rgbcolor='red')+list_plot([i for i in list2], rgbcolor='orange')+...
</code></pre>
https://ask.sagemath.org/question/56240/automatically-adding-colors-to-multiple-list_plot-graphics/?answer=56242#post-id-56242One way would be to use the hue.
Example:
sage: tau = 2*pi
sage: ngon = lambda n: [(n*cos(k*tau/n), n*sin(k*tau/n)) for k in range(n)]
sage: point_lists = [ngon(n) for n in range(3, 13)]
sage: n = len(point_lists)
sage: G = sum(list_plot(point_list, hue=k/n) for k, point_list in enumerate(point_lists))
sage: G.show(aspect_ratio=1)
![Sum of list_plot graphics with varying hue](/upfiles/16161184304861988.png)Fri, 19 Mar 2021 02:48:00 +0100https://ask.sagemath.org/question/56240/automatically-adding-colors-to-multiple-list_plot-graphics/?answer=56242#post-id-56242Answer by Juanjo for <p>Is there a way to <em>automatically</em> color different plots?</p>
<p>For example, I'm trying to plot complex points using <code>list_plot</code>, and the points are all stored in a list of lists, each of which I would like to be a different color. So, in order to plot these points, I have </p>
<pre><code>list=[list1,list2,...]
plot = sum(list_plot(i) for i in list)
</code></pre>
<p>I'm wondering if there's any (somewhat simple) way to add different colors corresponding to each list in an iterable fashion, rather than typing out </p>
<pre><code>plot=list_plot([i for i in list1], rgbcolor='red')+list_plot([i for i in list2], rgbcolor='orange')+...
</code></pre>
https://ask.sagemath.org/question/56240/automatically-adding-colors-to-multiple-list_plot-graphics/?answer=56254#post-id-56254Instead of the hue, as suggested by @slelievre, you could use colors extracted from a color map. For example, borrowing and slightly modifying slelievre's code, you could write:
tau = 2*pi
ngon = lambda n: [(n*cos(k*tau/n), n*sin(k*tau/n)) for k in range(n+1)]
point_lists = [ngon(n) for n in range(3, 13, 2)]
n = len(point_lists)
ops = dict(plotjoined=True, markeredgecolor="black", marker="s")
col = lambda c: colormaps.Dark2(c)[0:3]
G = sum(list_plot(point_list, color=col(k/n), **ops)
for k, point_list in enumerate(point_lists))
G.show(aspect_ratio=1,frame=True)
![image description](/upfiles/16161663453136413.png)
You could replace `colormaps.Dark2` by, say, `colormaps.jet`, `colormaps.Blues`, etc. For a complete list of predefined color maps, type `list(colormaps)`.Fri, 19 Mar 2021 16:06:14 +0100https://ask.sagemath.org/question/56240/automatically-adding-colors-to-multiple-list_plot-graphics/?answer=56254#post-id-56254