2
$\begingroup$

Can I use ColorFunction in place of color in SwatchLegend or is there any way to include ColorFunction in SwatchLegend?

For example, the command

SwatchLegend[{Directive[{"Rainbow", Opacity[0.25]}]},
  {Text[Style["f1", FontFamily -> "Times New Roman", 18, Red]]}]

shows only gray color.

$\endgroup$

2 Answers 2

0
$\begingroup$

Update 2: In version 10.0+, you can also use LinearGradientImage:

Labeled[LinearGradientImage["Rainbow", ImageSize -> {30, 30}], 
 Style["f1", 18, Red, FontFamily -> "Times New Roman"], Top]

enter image description here

Update: "want the color gradient of "Rainbow" in a single box for single label":

Labeled[ImageResize[ColorData["Rainbow", "Image"], {30, 30}], 
 Style["f1", 18, Red, FontFamily -> "Times New Roman", ShowStringCharacters -> False], 
 Top]

enter image description here

Use

Show[ImageResize[ColorData["Rainbow", "Image"], {30, 30}], BaseStyle -> Opacity[.5]]

as the first argument of Labeled to get:

enter image description here

Original answer:

labels = {"f1", "f2", "f3", "f4"};

SwatchLegend["Rainbow", labels, BaseStyle -> Opacity[.25], LegendMarkerSize -> 30]

enter image description here

SwatchLegend["Rainbow", Style[##, FontFamily -> "Times New Roman", 18] & @@@ 
  Transpose[{labels, ColorData["Rainbow"] /@ Subdivide[Length@labels - 1]}], 
 BaseStyle -> Opacity[.25], LegendMarkerSize -> 30]

enter image description here

$\endgroup$
4
  • $\begingroup$ Thank you.. But I want the color gradient of "Rainbow" in a single box for single label.. $\endgroup$
    – user61823
    Dec 10, 2018 at 11:56
  • $\begingroup$ @user61823, please see the update. $\endgroup$
    – kglr
    Dec 10, 2018 at 20:44
  • $\begingroup$ How can I use the "Labeled" Command for two images that can be displayed in one column...?? The output will be the same as that of SwatchLegend one having the "Rainbow" color and the other .. say Red.. and the labels will be f1, f2.. at the right position for each color.. $\endgroup$
    – user61823
    Dec 11, 2018 at 13:41
  • $\begingroup$ @user61823, you can try something like: Column[{Labeled[ImageResize[ColorData["Rainbow", "Image"], {30, 30}], Style["f1", 18, Blue, FontFamily -> "Times New Roman", ShowStringCharacters -> False], Right], Labeled[Graphics[{Red, Rectangle[]}, ImageSize -> {30, 30}, PlotRangePadding -> 0, ImagePadding -> 0], Style["f2", 18, Red, FontFamily -> "Times New Roman", ShowStringCharacters -> False], Right]}, Alignment -> Left] $\endgroup$
    – kglr
    Dec 11, 2018 at 15:49
1
$\begingroup$
SeedRandom[0]

data = RandomReal[100, 10]

(* {65.2468, 63.307, 68.2813, 56.6352, 93.5202, 97.6188, 23.8452, \
63.7562, 10.1098, 64.5525} *)

{min, max} = MinMax[data]

(* {10.1098, 97.6188} *)

Format[f[n_]] := Subscript[f, n]

SwatchLegend[
 ColorData["Rainbow"][Rescale[#, {min, max}]] & /@ data,
 f /@ Range[Length[data]],
 LegendLayout -> "Row"]

enter image description here

EDIT: Or

SwatchLegend[ColorData["Rainbow"][#] & /@ Range[0, 1, 1/9], 
 f /@ Range[10], LegendLayout -> "Row"]

enter image description here

EDIT 2: It appears from your comment that you want a BarLegend rather than a SwatchLegend.

BarLegend["Rainbow", 
 LegendLabel -> Text[Style["f1", FontFamily -> "Times New Roman", 18, Red]], 
 LegendLayout -> "Row"]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you.. But I want the color gradient of "Rainbow" in a single box for single label.. $\endgroup$
    – user61823
    Dec 10, 2018 at 11:56

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.