# How do I get such colorful plots with bar legends colored like this?

Each line in this plot is for the same equation with different values of a parameter $$\beta$$; the range of $$\beta$$ is shown in the lines plotted and the bar legend. I want to know how to create such colorful plots. Any kind of example would be nice. Thank you.

Let's say I want to plot this equation:

\rho == (C (3 + C r^2))/(8 \[Pi] (1 + C r^2)^2 (1 + \[Beta]))


with C = 0.00662391, r = [0, 12.5] and β = [0,0.5, 0.05].

Something like this?

c = 0.00662391;
ρ[r_, β_] = c (3 + c r^2)/(8 π (1 + c r^2)^2 (1 + β)) ;

{βmin, βmax, βinc} = {0, 0.5, 0.05};

colorscheme = "SolarColors";

βvals = Range[βmin, βmax, βinc];

βcolors = ColorData[colorscheme] /@ ((βvals - βmin)/(βmax -  βmin))

Plot[Evaluate[Table[ρ[r, β], {β, βvals}]], {r, 0, 12.5},
PlotStyle -> βcolors,
PlotLegends -> BarLegend[{colorscheme, {βmin, βmax}}]]


The main idea here is to pre-calculate the $$\beta$$ values that will be used, feed them into a pre-defined color function to figure out the corresponding colors, and then supply the colors (and the legend) as options to Plot.

EDIT: To include dashings, replace the last few lines of the code with something like the following. The list dashings should be a list, with the same length as βcolors, specifying the dashing you want for each curve. In the case below, I create a list that contains Dashed repeatedly, so that each curve becomes dashed.

    βcolors = ColorData[colorscheme] /@ ((βvals - βmin)/(βmax -  βmin));
dashings = ConstantArray[Dashed, Length[βvals]];

Plot[Evaluate[Table[ρ[r, β], {β, βvals}]], {r, 0, 12.5},
PlotStyle -> Transpose[{βcolors, dashings}],
PlotLegends -> BarLegend[{colorscheme, {βmin, βmax}}]]


• Thanks a lot. That was helpful. But I can not use another PlotStyle other than the default one. Can you please help? Commented Jun 23, 2023 at 17:55
• @BibhashDas I'm not clear on what you're asking for. What PlotStyle do you want to use? What do you want to accomplish? Commented Jun 23, 2023 at 18:12
• I want to use PlotStyles like Dashed or DotDashed. Commented Jun 23, 2023 at 18:43
• @BibhashDas: See my edited answer. Commented Jun 23, 2023 at 18:56
• @MichaleSeifert It worked. Thanks. Commented Jun 24, 2023 at 18:54
\$Version

(* "13.2.1 for Mac OS X ARM (64-bit) (January 27, 2023)" *)

Clear["Global*"]


C has a built-in meaning, use c instead

c = 0.00662391;

ρ = (c (3 + c r^2))/(8 π (1 + c r^2)^2 (1 + β));


With a BarLegend

Plot[Evaluate@Table[
Tooltip[10^4 ρ,
StringForm["=", HoldForm[β],
NumberForm[β, {5, 2}]]], {β, 0, 0.5, 0.05}],
{r, 0, 10},
PlotStyle -> (ColorData["SolarColors"][2 #] & /@
Range[0, 0.5, 0.05]),
Frame -> True,
FrameLabel -> {
StringForm[" (km)", HoldForm[r]],
StringForm[" (x\[ThinSpace])", HoldForm[ρ], 10^"-4", km^"-2"]},
PlotLegends -> BarLegend[{"SolarColors", {0, 0.5}},
LegendLabel -> Style[HoldForm[β], 14]]]


With a LineLegend

Plot[Evaluate@Table[Tooltip[10^4 ρ,
StringForm["=", HoldForm[β],
NumberForm[β, {5, 2}]]], {β, 0, 0.5, 0.05}],
{r, 0, 10},
Frame -> True,
FrameLabel -> {
StringForm[" (km)", HoldForm[r]],
StringForm[" (x\[ThinSpace])", HoldForm[ρ], 10^"-4", km^"-2"]},
PlotLegends -> LineLegend[Range[0, 0.5, 0.05],
LegendLayout -> {"Column", 2},
LegendLabel -> Style[HoldForm[β], 14]]]
`

• Thank you for your help. Commented Jun 24, 2023 at 18:55