Tag Info

Hot answers tagged

80

In case you want more flexibility, it's also possible to design your own legends, for example along the lines of this MathGroup post. For your example, the process would start with the function legendMaker. Instead of repeating the same definition as in the above post, I've overhauled legendMaker in response to image_doctor's answer, to separate out the ...


34

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


31

Use the individual legends as tick labels: dates = Through[{First, Last}@#] & /@ res {{1769, 1821}, {1775, 1817}, {1770, 1831}, {1818, 1883}, {1777, 1855}, {1766, 1817}, {1870, 1924}} names={"Napoleon Bonaparte", "Jane Austen", "Hegel", "Marx", "Gauss", "Madame de Stael", "Lenin"}; llpd = MapIndexed[Thread@{#, First@#2} &, dates]; legends ...


30

You can make use of the following options in Plot, e.g. : Plot[ Tooltip @ {x^2, x^3, x^4}, {x, -2, 2}, PlotStyle -> {Red, Green, Blue}, PlotRangePadding -> 1.1] /. {Tooltip[{_, color_, line_}, tip_] :> {Text[Style[tip, 14], {.1, 0} + line[[1, -1]]], color, line}} Edit Since there was another ...


24

While in many ways Mathematica's graphics is miles ahead of its competition, the supplied legend package looks like a hack that does not belong. In its own documentation, under Inset, you will find examples of better looking legends. Building on those examples, I have a simple legend function. ClearAll[simpleLegend] ...


22

In this case I would use Point for plotting the points. For example n = 5000; pos = RandomVariate[NormalDistribution[0, 2], {n, 2}]; altitude = Norm /@ pos; colorf = Blend[{{Min[altitude], Yellow}, {Max[altitude], Red}}, #] & pl = Graphics[MapThread[{colorf[#1], Point[#2]} &, {altitude, pos}], Axes -> True, AspectRatio -> 1] As for ...


17

A small enhancement to Jens's excellent response is to generalise the options handling by defining the options for legendMaker to include all those applicable to Framed. Options[legendMaker]=Options@Framed; legendMaker[lineDirectives_, markerSymbols_, textLabels_, opts : OptionsPattern[]] := Module[{f, g}, f = Grid[MapThread[{Graphics[{#1, ...


17

Here is an interactive version, with definition below. A functional plot, functionplot=Plot[{Sin[x],Cos[x]},{x,0,2\[Pi]}, Frame->{True,True,False,False}, FrameLabel->{"x","y(x)"}, FrameStyle->Directive[13,Italic], PlotStyle->Thick, PlotRangeClipping->False, PlotRange->{-1.2,1.2}, ...


16

One way of doing it is to use Row instead of GraphicsRow and setting an explicit ImageSize. The solution also scales for different sizes. Setting the same ImagePadding on both plots will ensure that they are nicely aligned. For example: With[{size = 300}, Row[Show[#, ImageSize -> {Automatic, size}, ImagePadding -> 20] & /@ {DensityPlot[ ...


16

ListLinePlot (or alternately ListPlot with Joined->True) accepts a ColorFunction, which you can use to color your points. The lines can be later converted to points as: colorFun = Function[{x, y}, Blend[{{Min[altitude], Yellow}, {Max[altitude], Red}}, Norm[{x, y}]]]; ListLinePlot[pos, ColorFunction -> colorFun, AspectRatio -> 1, ...


15

As usual Heike has a fine method, but I can tighten it up. This will render quite a bit faster, it will use built-in color functions more easily, and it will IMO interactively rescale better. Data: n = 5000; pos = RandomReal[NormalDistribution[0, 2], {n, 2}]; altitude = Norm /@ pos; colorf = Blend[{Yellow, Red}, #] &; Legend function (modified): ...


15

It turns out that the Spacings 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, ...


15

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


14

When Show is used the legends use ShowLegend. Here is a demo: depth4 = Range[20]^3; plot = ListLogLogPlot[Sort[depth4], PlotRange -> {{1, 50000}, {1, 50000}}, Joined -> True, PlotStyle -> {Purple}, BaseStyle -> {FontSize -> 14}]; line = LogLogPlot[11024 x^(-0.94232), {x, 1, 100000}, PlotStyle -> {Orange, Dashed, Thick}]; line2 = ...


14

Histogram[{bottom, middle, top}, 10, ChartLayout -> "Stacked", ChartLegends -> {"Bottom", "Middle", "Top"}] /. Column[l_List] :> Column[Reverse@l]


14

Bob Hanlon's answer works very well, but in some ways it is the hard way of doing things. If you have v9 or v10, then it is arguably easier to use the legend constructs within it. Similar to his answer, we get the image and element names: img = Import["ExampleData/1PPT.pdb", "Rendering" -> "BallAndStick", ImageSize -> 500]; elements = ...


13

Try using Style in the option values for PlotLegend->{...}. For example: Plot[{Sin[x], Cos[x]}, {x, 0, 2 Pi}, PlotLegend -> {Style["sine", Red, Bold, 18], "cosine"}, LegendLabel -> None] gives:


13

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


13

We may observe that the automatically generated legend limits the number of legend items to the number of available colors in the given color scheme. Using this utility function: plot[scheme_] := Plot[Evaluate[Range[20] + x], {x, -5, 5}, PlotLegends -> "Expressions", PlotStyle -> scheme] Observe the result for indexed color scheme #42 which has ...


12

You can also use Epilog->Inset[...] as follows: For depth4 = Range[20]^3; plot = ListLogLogPlot[Sort[depth4], PlotRange -> {{1, 50000}, {1, 50000}}, Joined -> True, PlotStyle -> {Purple}, BaseStyle -> {FontSize -> 14}]; line = LogLogPlot[11024 x^(-0.94232), {x, 1, 100000}, PlotStyle -> {Orange, Dashed, Thick}]; line2 ...


12

Perhaps this example will help you? data = {{1, 2}, {3, 4}, {3, 5}, {2, 3}}; Needs["PlotLegends`"] ListLinePlot[Table[{#1, Log[b, #2]} & @@@ data, {b, 3, 10, 2}], PlotLegend -> {"A", "B", "C", "D"}, LegendPosition -> {1.1, -0.4}] Addressing your comment, to only specify the iterator once: data = {{1, 2}, {3, 4}, {3, 5}, {2, 3}}; ...


12

If your data is Imported say in data then you can use data[[All,{1,2}]] to use the first column as x and the second as y value. Plotting all three data sets would be ListPlot[{data[[All, {1, 2}]], data[[All, {1, 3}]], data[[All, {1, 4}]]}] If you want to print against row index, like in yopur figure you can use ListPlot[{data[[All, 2]], data[[All, 3]], ...


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


12

Two work-arounds: (1) Use TickLabels BarLegend[{"Rainbow", {-0.015, 0.015}}, ImageSize->300, Charting`TickLabels -> (Style[NumberForm[#, {Infinity, 3}],Bold,Black,12] & /@ Range[-.015, .015, .005])] (2) Use LabelingFunction: BarLegend[{"Rainbow", {-0.015, 0.015}}, ImageSize->300, LabelingFunction -> (Style[NumberForm[#, ...


11

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


11

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[ Graph | Graphics | Graphics3D, legend ] Then, in your case, you use ...


11

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


11

This is an intentional change to make PlotLegends -> "Expressions" more consistent with PlotLegends -> Automatic. Both now do not produce legends when only one line is present. What you are looking for is PlotLegends -> "AllExpressions" which has the old behavior, e.g. Plot[x, {x, 0, 1}, PlotLegends -> "AllExpressions"] More generally, ...


11

Instead of simply using Red as the directive, set the face and edge colors explicitly so that there is no ambiguity. If you use FaceForm@Red and EdgeForm@Red (or None) in your definitions for redDisk and redRectangle, you get legend markers without black borders.



Only top voted, non community-wiki answers of a minimum length are eligible