Tag Info

Hot answers tagged

77

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


33

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


27

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


23

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


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

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


15

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


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

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]


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:


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

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


11

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


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

If you have your own color function, then passing an explicit BarLegend to PlotLegends seems to be the only way out. This also means that you'll have to feed the min/max for your data, but this isn't hard. Here's an example: With[{cf = Blend[{Blue, White, Red}, #] &, data = Table[Sin[x y], {y, 0, 3, 0.1}, {x, 0, 3, 0.1}]}, ListPlot3D[data, ...


11

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


10

You can overlap GraphicsRow items by specifying negative spacings. This omits whitespace to the right (as in kguler's answer): size = 360; colorbar[colorFunction_: Automatic, divs_: 25] := DensityPlot[y, {x, 0, .1}, {y, 0, 1}, AspectRatio -> Automatic, PlotRangePadding -> 0, PlotPoints -> {2, divs}, MaxRecursion -> 0, FrameTicks -> ...


10

I use a homebrew solution, called as follows: Show[Plot[{x, x^2}, {x, 0, 1}, PlotStyle -> {Red, Blue}], tCustomLegend[{tCustomLegendItem[Line, x, PlotStyle -> Red], tCustomLegendItem[Line, x^2, PlotStyle -> Blue]}, {0.2, 0.8}]] Giving The full code is: tYellow=RGBColor[1,0.8,0.2]; tColorList=ColorData[3,"ColorList"]; ...


10

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


10

If you're willing to abandon PlotLegends (which tkott advises in the comment and many others would advise too), then you could work with the code posted in this related answer to achieve some customization. You'll have to execute the definitions in that post before trying the lines below. Take the following two plots which generate the same curve first as ...


10

Using belisarius' method, if you want to reverse the order for all vertical legends you could use Legending`LegendContainer like this: {bottom, middle, top} = RandomReal[NormalDistribution[0, 1], {3, 200}]; SetOptions[Legending`GridLegend, Legending`LegendContainer -> (Framed[# /. Column[l_List] :> Column[Reverse@l]] &) ]; Histogram[{bottom, ...


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

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


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.



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