7
$\begingroup$

example

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].

$\endgroup$

2 Answers 2

12
$\begingroup$

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}}]]

enter image description here

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}}]]

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

enter image description here

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]]]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you for your help. $\endgroup$ Commented Jun 24, 2023 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.