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


28

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


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

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

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

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]


14

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


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


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


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


12

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


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

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

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


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



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