Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.
97

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


44

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}} Update (05.02.2016) Tried the ...


40

Giving the density plot a logarithmic scale must always involve - unless some future version of Mathematica includes it by default - overriding the ColorFunctionScaling of the original plotting command and supplying a custom scaling function. The simplest logarithmic scaling is of the form $$ \mathrm{scaling}(x)=\frac{\log(x/\mathrm{min})}{\log(\mathrm{max}/ ...


37

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


36

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


33

This is how to save the graph, legend and all: Another way would be to use Rasterize: Rasterize[Plot[...]] The legend and the graph is now one image.


30

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


29

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] simpleLegend[legendItems__,pos_]:=Module[{...


25

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


25

There is an undocumented form of LegendLayout that is useful here: LegendLayout -> {"Column", noOfColumns} similarly for "Row" and their reversed cousins. So, in your case I would use LegendLayout -> {"Column", 2} giving Obviously, you do not have to include the color list when you pass it to PlotLegends.


24

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:


24

Tell it that you really want n rows by {"Row", n}, for example: LineLegend[{Blue, Orange, Green}, {"this is a big test", "this is a big test", "this is a big test"}, LegendLayout -> {"Row", 1}]


23

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


22

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


21

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


21

As it often happens, when I was researching for writing a fine question I also digged through the documentation and found the answer. There is an option LegendMarkerkSize which in my opinion has an unintuitive name in the case of the BarLegend. Anyway it is exactly meant to do what I needed: testPlot = ArrayPlot[ Array[RandomInteger[100] &, {100, 100}]...


20

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


20

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, Line[{{-....


20

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


19

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


18

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 = Import["...


18

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


17

Since BubbleChart does the scaling of the bubbles the trick is to let it draw the circles of the legend too. I do that by adding the legend circles to the plot data. The rest of the graphics can be added using Show. lc = Append[Reverse@CityData[#, "Coordinates"], CityData[#, "Population"] ] & /@ CityData[{Large, "USA"}]; steps = 8; ...


17

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


17

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


17

I consider myself an amateur. I was also interested in this question, and I found the following code solves the problem. However, the command that solves the problem ("Ticks") shows up highlighted in red. You can change many of the features of the legend, like size, add a label, etc. Legended[ ListDensityPlot[Table[ArcTan[Cos[x + y], Sin[x + y]], {x, ...


17

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.


17

There is an easier answer to this (esp. if you don't want to custom-specify colors), at least in version 10, found as the answer to this question: LegendLayout -> {"Column", 1}] All credit goes to MinHsuan Peng who answered that question, I just reposted here because I found this page first and thought other people searching for this would like to ...


16

Many legends can be easily created using combination of Row and Column, where for example, each Row contains two elements: a Graphics expression, and a text label which need not be wrapped in Text. However, since the label you want involves size coding, this method doesn't work. Further, you want to match the size of Disks in the main graphics to the size ...


16

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


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