Hot answers tagged legending
25
Edit
I updated the definitions of reportColorRange and colorLegend: added more comments in the code, allowed more customization options for the legend. Color gradients are produced by VertexColors for better-looking PDF export; gradients can also be replaced by color bands (using the "ColorSwathes" option). The labels on the color bar can be specified by ...
12
There is LabelStyle option for these new legends.
For example:
Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]},
PlotLegends ->
LineLegend["Expressions",
LabelStyle -> {FontFamily -> "Helvetica"}]]
12
Even though the documentation for SmoothHistogram doesn't explicitly mention PlotLegends as an option, it is a valid option and works as expected:
SmoothHistogram[
Table[RandomVariate[NormalDistribution[c, 1], 500], {c, 0, 3}],
PlotLegends -> {"0", "1", "2", "3"}]
In general, it's a good idea to try out options that you think should work (for ...
10
Given that plot legend question keep arising I thought I would share my approach to legend positioning. I want to be able to use the legend as a locator and move it to the exact position I want it.
pt = Scaled[{0.5, 0.5}];
(* image padding for the ListLinePlot *)
{{l, r}, {b, t}} = {{20, 100}, {100, 10}};
(* width and height of the ListLinePlot *)
{w, h} = ...
10
You can modify the "Legended" style
A simple way to accomplish this for a particular notebook is to Format->Edit Stylesheet
Then in the InputField on top, you write Legended and press enter. A style cell will appear in the stylesheet. You then select that cell, go to the Options Inspector and modify the style at will.
10
Perhaps just defining a custom legend name:
myStyle[labels_, opts : OptionsPattern[]] :=
LineLegend[labels, opts,
LabelStyle -> {FontFamily -> "Arial", FontSize -> 20}, LegendFunction -> "Frame",
Background -> Directive[LightGray, Opacity[0.3]]
]
Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]}, PlotLegends -> myStyle[{"one", "two"}]]
...
10
You can put the SetOptions instruction into your init.m file and then it will always be used. Just evaluate this once and you are all set:
(Export[#,
Import[#, "Text"] <>
"\n\nSetOptions[LineLegend,LabelStyle\[Rule]{FontFamily\[Rule]\"\
Comic Sans MS\",Red}]", "Text",
CharacterEncoding :> $CharacterEncoding]) ...
10
You can't actually tell the LineLegends construct what the BaseStyle option of the enclosing plot is. But you can set the BaseStyle option for the LineLegend as well. Do not be fooled by the red text when you type BaseStyle inside the LineLegend! This is just another of those cases where things work even though they are not documented and show up in red.
...
10
There is absolutely nothing to stop you setting your legend up this way using a custom plot marker in a SwatchLegend. For example, consider some data:
fakedata =
Table[FoldList[0.99 #1 + #2 &, 0.,
RandomVariate[NormalDistribution[0, 1], {99}]], {6}];
Create a custom marker:
blob = Graphics[{AbsoluteThickness[1.4], Line[{{-0.5, 0}, {0.5, 0}}], ...
9
Elaborating a little on Sjoerd's answer:
Specifying LegendMargins makes it even clearer that the fancy new legend might be not as useful as I first thought. We now have a working legend but lost basically all control over its apearance:
DensityPlot[Sin[x] Sin[y], {x, -4, 4}, {y, -3, 3},
ImageSize -> {300, 300},
ImagePadding -> {{30, 30}, {30, ...
9
Thanks to Nasser M. Abbasi i found a way. To change the Display. The function that you can provide for any ~Legend via LegendFunction wraps the complete ~Legend[] into anything. And he mentioned, that NumberForm encapsulates the numbers. So why not replace them (delayed)?
Version 1: Scientific Notation at every Label
Hence choosing
f[x_] := x /. ...
9
It turns out that the Spacing option does exactly what is needed, even though its use in legend constructs such as PointLegend is not documented, and it shows as red text when you use it in those constructs.
PointLegend[{Red, Blue}, {"Series 1", "Series 2"},
LegendMarkers -> {{"\[FilledCircle]", 20}, {"\[FilledCircle]", 20}},
Spacings -> {0.2, ...
9
As Cormullion pointed out, you will have to create the legend by hand as it would be difficult to have a legend created automatically by GraphicsGrid. However, the simplest method of attaching a legend is to use Legended directly. The structure of Legended when used like this is
Legended[ Graphics | Graphics3D, legend ]
Then, in your case, you use
...
8
If you provide BarLegend as an explicit option value of PlotLegends it seems to do what you want:
DensityPlot[Sin[x] Sin[y], {x, -4, 4}, {y, -3, 3},
ImageSize -> {300, 300}, PlotLegends -> BarLegend["LakeColors"]]
8
The reason for this is that the output in the documentation is rasterized. It is just an image, not the actual object that Plot would produce.
Legends are contained in a Legended expression, not in Graphics, which I agree does bring a number of difficulties when using these plots.
8
The answer is to replace "Expressions" with the more controllable LineLegend["Expressions"] as you can pass in options, such as LegendFunction:
PlotLegends -> LineLegend["Expressions",
LegendFunction -> (Pane[#, ImageSize -> {100, 100}] &)]
and for a higher value of t
8
You also aren't getting the right line thickness in your legend. I'd suggest a slightly different route, of creating custom legend markers, similar to the method described in this answer, and including them in a SwatchLegend.
legmarkers = MapThread[Graphics[{#1, AbsoluteThickness[2],
Line[{{-1, 0}, {1, 0}}], #2}] &, {col, {Disk[{0, 0}, 0.3],
...
8
The package PlotLegends` is obsolete in v9, and so is the package Graphics`Legend` that the Mie code notebook was using. Since you are running v9, you might as well use the new stuff which is integrated much better. Of course, as there are several plots to combine, the method is going to look similar, but not as convoluted as the old form. The high level ...
7
The message "An option must be a rule or a list of rules..." tells you that PlotLegend should be a rule, not a function call. In other words, you should write something like
Plot[{u, v, w, y}, {x, -10, 10}, PlotRange -> All, PlotStyle -> {AbsoluteDashing[{}],
Dashed, DotDashed, Dotted}, PlotLegend->{{Graphics[{AbsoluteDashing[{}],
Line[{{7, 0.1}, ...
7
The expression that is plotted in the legend are the provided pure functions (with some scoped internal variable names). PlotLegends is somewhat confused so we have to provide explicit entries for the Legend. Since the desired result is somewhat unclear here a few suggestions:
v[x_, k_] := k*x^-k;
g[x_, k_] := (x^-k)*Sin[x^k]/(1 + x^k);
f[x_, k_] := x^k/(1 ...
6
Here is a transcription of my related answer to the problem at hand - hopefully I understand the question correctly:
Take an example data set:
t = Flatten[
Table[{x, y, 2/((x - 1)^2 + y^2)}, {x, -5, 5, .3}, {y, -5, 5, .3}],
1];
{plot, colors, range} = reportColorRange[ListDensityPlot[t]]
Use the function definitions I posted earlier, except that ...
6
For the first part of your question, you can specify a function for the FrameTicks or Ticks option. This function will then be applied to xmin and xmax (or ymin and ymax for the tick marks along the vertical axis). This means that you can let this function take care of the placement of the tick marks automatically without having to worry about different plot ...
6
Sorry for the misunderstanding, I think this might now do what you want.
data = RandomReal[{0, 1}, {10, 10}];
plot = ListDensityPlot[data, PlotRange -> Automatic, ColorFunction -> GrayLevel];
coords = List @@ InputForm[plot][[1, 1, 1]];
colorvals = List @@ InputForm[plot][[1, 1, 3, 2]];
max = Position[colorvals, Max@colorvals][[1, 1]];
min = ...
6
This problem often occurs in plotting and graphics because of the use of Foo instead of AbsoluteFoo for the directives. The former uses sizes relative to the plot size, whereas the latter, as the name suggests, uses absolute sizes.
In the built-in plot legends in version 9, the plot and the legend are actually two separate objects which are just combined, ...
6
Best I could do. GraphicsGrid can't read my mind at all...
legend = BarLegend[{"LakeColors", {-1, 1}}, LegendLayout -> "Row"];
plots = Table[
DensityPlot[Sin[i x] Sin[y/i], {x, -4, 4}, {y, -3, 3}], {i, 1,
4}];
Column[{
GraphicsGrid[{
{plots[[1]], plots[[2]]},
{plots[[3]], plots[[4]]}}, ImageSize -> 300],
legend}, Center]
6
This first solution is not exactly what you're looking for, but it gets close:
BarChart[{{1, 2, 3}, -{4, 5, 6}},
ChartLabels -> {Placed[{"r1", "r2"}, {.5, 2}],
Placed[{"c1", "c2", "c2"}, Axis]}]
To get the correct representation you need to tweak a bit some values:
BarChart[{{1, 2, 3}, -{4, 5, 6}},
ChartLabels -> {Placed[{"r1", "r2"}, ...
5
This solution will no longer work in Mathematica 9 because they now use SwatchLegend in FullForm. Instead, apply this:
Histogram[{bottom, middle, top}, 10, ChartLayout -> "Stacked",
ChartLegends -> {"Bottom", "Middle", "Top"}] /.
SwatchLegend[l1_List, l2_List, r1_Rule, r2_Rule] :>
SwatchLegend[Reverse[l1, 1], Reverse[l2, 1], r1, r2]
The ...
5
Here's another solution providing interactive labeling functionality similar to JxB's answer. That is, you can hover over the curves to see their label as a Tooltip, but then click at any point on the curve to make the label stick permanently to that position:
Options[burnTooltips] = {ImageSize -> 360,
"LabelFunction" -> (Framed[#, FrameStyle ...
5
Use MapAt to apply the Legended and Style wrappers directly to the last item in the dataset:
data = RandomVariate[NormalDistribution[0, 0.6], 40];
data = MapAt[Legended[Style[#, Orange], "Preliminary estimate"] &, data, -1];
BarChart[data, ChartStyle -> Green, LegendAppearance -> "Row"]
5
For your first question:
sampleData = Transpose[{RandomVariate[NormalDistribution[0, 3*10^-6], 200],
RandomVariate[NormalDistribution[0, 3*10^-6], 200]}];
hist =
DensityHistogram[sampleData, binning, LabelingFunction -> None,
PerformanceGoal -> "Speed", ColorFunction -> (If[1 - #1 === 0, White,
...
Only top voted, non community-wiki answers of a minimum length are eligible

