Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Multiple Line coloring and Dashing in ListPlot

1,781 views
Skip to first unread message

graser

unread,
Jul 3, 2013, 4:52:05 AM7/3/13
to
Dear Mathematica Users,

I have a question about the coloring and dashing lines in listplot.
For example, Let's say there is a multiple data set like
data = Table[RandomReal[{2 i, 2 i + 1}, {20}], {i, 9}];

I want to plot them in listplot or listloglogplot with all different color and all different dashing with (joined -> True).
How can I do it automatically or minimum option?
The default number of coloring in mathematica (I am in still 8) is 4. after that, it repeat same color.

please help me!

Thanks!


Bob Hanlon

unread,
Jul 3, 2013, 6:03:39 PM7/3/13
to

The colors do not repeat but the differences are subtle. Using PlotMarkers
is easier than varying dashing.


$Version


"8.0 for Mac OS X x86 (64-bit) (October 5, 2011)"


data = Table[RandomReal[{2 i, 2 i + 1}, {20}], {i, 9}];


llp1 = ListLinePlot[data, PlotMarkers -> Automatic]


llp2 = ListPlot[data, PlotMarkers -> Automatic, Joined -> True]


There are nine different colors in each plot


(Cases[#, Hue[__], Infinity] // Union // Length) & /@ {llp1, llp2}


{9, 9}



$Version


"9.0 for Mac OS X x86 (64-bit) (January 24, 2013)"


data = Table[RandomReal[{2 i, 2 i + 1}, {20}], {i, 9}];


llp1 = ListLinePlot[data, PlotMarkers -> Automatic]


llp2 = ListPlot[data, PlotMarkers -> Automatic, Joined -> True]


There are nine different colors in each plot


(Cases[#, RGBColor[__], Infinity] // Union // Length) & /@ {llp1, llp2}


{9, 9}



Bob Hanlon

Alexei Boulbitch

unread,
Jul 5, 2013, 5:57:51 AM7/5/13
to
Dear Mathematica Users,

I have a question about the coloring and dashing lines in listplot.
For example, Let's say there is a multiple data set like
data = Table[RandomReal[{2 i, 2 i + 1}, {20}], {i, 9}];

I want to plot them in listplot or listloglogplot with all different color and all different dashing with (joined -> True).
How can I do it automatically or minimum option?
The default number of coloring in mathematica (I am in still 8) is 4. after that, it repeat same color.

please help me!

Thanks!



There are several such possibilities to make different colors. The most straightforward is as follows:

data = Table[RandomReal[{2 i, 2 i + 1}, {20}], {i, 9}];

ListPlot[data, Joined -> True, PlotStyle -> Table[Hue[i/9], {i, 1, 9}]]

Try it.

You may use the RGBColor as well:

ListPlot[data, Joined -> True, PlotStyle -> Table[RGBColor[i/9, 0, 1 - i/9] , {i, 1, 9}]]

try various combinations of colour variation: RGBColor[i/9, 0, 1], RGBColor[0, 0, 1 - i/9],
RGBColor[0, i/9, 1 - i/9] and so on. Try it.


You may as well use a ColorData:

ListPlot[data, Joined -> True, PlotStyle -> Table[ColorData[3, "ColorList"][i/9], {i, 1, 9}]]

and look, e.g. into ColorData with different numbers.



However, if it is something I really care how it looks like, I do it "by hand":

ListPlot[data, Joined -> True, PlotStyle -> {Red, Blue, Green, Brown, Pink,Cyan, Magenta, Gray,
Orange}]

You have also possibility of using Darker[ ] function:

ListPlot[data, Joined -> True, PlotStyle -> {Red, Darker[Red, 0.5], Blue, Darker[Blue, 0.5], Green,
Darker[Green, 0.5], Pink, Darker[Pink, 0.5], Cyan}]

Try it.

Have fun. Alexei



Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone : +352-2454-2566
Office fax: +352-2454-3566
mobile phone: +49 151 52 40 66 44

e-mail: alexei.b...@iee.lu




Bill Rowe

unread,
Jul 5, 2013, 5:57:31 AM7/5/13
to
On 7/3/13 at 5:01 AM, gra...@gmail.com (graser) wrote:

>I have a question about the coloring and dashing lines in listplot.
>For example, Let's say there is a multiple data set like data =
>Table[RandomReal[{2 i, 2 i + 1}, {20}], {i, 9}];

>I want to plot them in listplot or listloglogplot with all different
>color and all different dashing with (joined -> True). How can I do
>it automatically or minimum option?

Here is an example of a list plot which has different dashing
and different colors

data = Table[RandomReal[{2 i, 2 i + 1}, {20}], {i, 3}];
ListPlot[data, Joined -> True,
PlotStyle -> {{Red, Dashing[.01]}, {Green, Dashing[.015]}, {Blue,
Dashing[.02]}}, Frame -> True]

Here, I have reduce the number of lines to be plotted for
clarity. You could replace the specific hard coded plot styles
with a list generated using Table, i.e., something like

styles=Table[{Hue[1/n],Dashing[5 n/1000]},{n,3}];
data = Table[RandomReal[{2 i, 2 i + 1}, {20}], {i, 3}];
ListPlot[data, Joined -> True, PlotStyle -> styles, Frame -> True]

>The default number of coloring
>in mathematica (I am in still 8) is 4. after that, it repeat same
>color.

Actually, version 8 does not repeat the same color after 4. But
the first color and 5 color with the default colors are quite
similar and difficult to distinguish.

By default the first color is

In[9]:= ColorData[1][1]

Out[9]= RGBColor[0.2472,0.24,0.6]

and the fifth color is:

In[10]:= ColorData[1][5]

Out[10]= RGBColor[0.24,0.353173,0.6]

A way to avoid this issue is to use one of the named color
schemes such as "Rainbow". See guide/ColorSchemes in the
documentation center. But do note for this to be visually
effective you need to map number of colors you want over the
range of 0 to 1. In fact, the short snippet of code I show above
for creating a list of styles using Table is fine for 3 colors
but will not be visually effective for a large number of colors.
There will be very little visual difference between Hue[1/n] and
Hue[1/(n+1)] when n gets large.


Nigel King

unread,
Jul 10, 2013, 3:23:10 AM7/10/13
to
All,
I wanted to extend the ideas from Bob and Alexei, because I have had trouble with doing this nicely.

$Version 9.0 for Mac OS X x86 (64-bit) (January 24, 2013)

(* When you want a set of legends one needs to extend the idea a little further *)
(* for a simple table of legends *)
legendTable[pairs_]:=TableForm[pairs,TableSpacing->{1,0.5}]
(* There seems to be no default for dashing so I define noDots *)
noDots=Dashing[{1,0}];
(* Create a general set of PlotStyles for some plots *)
colors={Red,Blue,Green,Brown,Pink,Cyan,Magenta,Gray,Orange};
dashings={noDots,DotDashed,Dotted,Dashed};
plotStyles=Flatten[Table[Directive[i,Thick,j],{j,dashings},{i,colors}]];

(* From an example by Bob Hanlon *)
plotList=Table[{i,RandomReal[{2 i,2 i+1},{20}]},{i,36}];
plots=plotList[[All,2]];
legends=plotList[[All,1]];

ListPlot[plots,
PlotStyle->plotStyles,
Joined->True,
PlotMarkers->Automatic,
ImageSize->600,
AspectRatio->1,
PlotLegends->Placed[LineLegend[plotStyles,legends,
LegendLabel->Style["Plots",16],
LegendFunction->"Frame",LegendMargins->5,LabelStyle->11,
LegendLayout->legendTable],After]]


For the function plots this example may give some useful ideas

(* an interesting example showing a number of techniques which I have learnt *)
(* Create a plot list in an easy to recognise form *)
plotList={
{Sin[x],"sin(x)",{Blue,noDots}},
{Cos[x],"cos(x)",{Red,noDots}},
{Tan[x],"tan(x)",{Green,noDots}},
{Cot[x],"cot(x)",{Brown,Dotted}},
{Sec[x],"sec(x)",{Pink,Dotted}},
{Csc[x],"csc(x)",{Cyan,Dotted}},
{Sinh[x],"sinh(x)",{Blue,DotDashed}},
{Cosh[x],"cosh(x)",{Red,DotDashed}},
{Tanh[x],"tanh(x)",{Green,DotDashed}},
{Coth[x],"coth(x)",{Brown,Dashed}},
{Sech[x],"sech(x)",{Pink,Dashed}},
{Csch[x],"csch(x)",{Cyan,Dashed}}
};

(* extract the plots *)
plots=plotList[[All,1]];
(* extract the legends *)
legends=plotList[[All,2]];
(* extract the plotStyles and control the thickness (in this case Medium is the default)*)
plotStyles=Map[Directive[Thickness[Medium],#[[1]],#[[2]]]&,
plotList[[All,3]]];
(* Modify legendTable to give two columns of functions *)
legendTable[pairs_]:=TableForm[Partition[pairs,6],
TableSpacing->{2,2,0.5},
TableDirections->{Row,Column,Row},
TableAlignments->{Left,Center}]

Plot[plots,{x,0,2Pi},PlotStyle->plotStyles,Frame->True,
PlotLegends->Placed[LineLegend[plotStyles,legends,
LegendLabel->Style["Plots",16],
LegendFunction->"Frame",LegendMargins->5,LabelStyle->11,
LegendLayout->legendTable],After],
ImageSize->500]

I hope this helps

Nigel King



On 3 Jul 2013, at 10:01, graser <gra...@gmail.com> wrote:

Dear Mathematica Users,

I have a question about the coloring and dashing lines in listplot.
For example, Let's say there is a multiple data set like
data = Table[RandomReal[{2 i, 2 i + 1}, {20}], {i, 9}];

I want to plot them in listplot or listloglogplot with all different color and all different dashing with (joined -> True).
How can I do it automatically or minimum option?
The default number of coloring in mathematica (I am in still 8) is 4. after that, it repeat same color.

please help me!

Thanks!






0 new messages