Tag Info

Hot answers tagged

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


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

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

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


10

bas = Import["ExampleData/1PPT.pdb", "Rendering" -> "BallAndStick", ImageSize -> 500]; elements = Import["ExampleData/1PPT.pdb", "ResidueAtoms"] // Flatten // Union; legend = GraphicsColumn[{ {Graphics[{#[[1]], Disk[{0, 0}, 1]}, ImageSize -> 10], #[[2]]} & /@ Thread[{ ElementData[#, "IconColor"] & /@ elements, ...


10

ListPlot[Table[f, {f, {Sin[x], Cos[x]}}, {x, 0, 2 π, 0.1}], PlotLegends ->PointLegend[ (Style[#, 40] & /@ {"sin(x)", "cos(x)"}),Alignment->Center]] Update: As noted by rcollyer in the comments Alignment is an undocumented option for PointLegend. As an alternative, the following old Pane trick works without relying on an undocumented option: ...


9

(This answer has just the circles, not the box with color scale information) To get circular looking disks I use Offset[r] for the radius, which ignores aspect-ratio and plot scale: Plot[x, {x, 0, 30}, AspectRatio -> 1/10, Epilog -> { {Red, Circle[{5, 5}, 5]}, {Green, Circle[{20, 20}, Offset[10]]} }] When putting Graphics together with ...


9

`AbsoluteThickne is another useful approach:e.g. op = Table[AbsoluteThickness[2], {3}]; leg = LineLegend[Automatic, {"Max", "Mea", "Min"}]; ListLinePlot[{historyobjetivomejor, historyobjetivomedia, historyobjetivopeor}, AxesLabel -> {"Generacion", "Objetivo"}, PlotStyle -> op, PlotLegends -> leg] Varying thickness: using: Manipulate[ ...


8

Not the prettiest, perhaps, but I just wanted to finish what I started. I was able to finish it after having read ssch excellent answer and see how he used Offset. This plot was truly a challenge to me, and the problem is in combining the different graphics. Had I tried this again I would probably do everything with Graphics and not use ListLinePlot at all. ...


8

If you look into the source code ClearAttributes[BarLegend, ReadProtected] ?? BarLegend And into nested functions (BarLegend,Charting`iBarLegend $\to$ Legending`LegendDump`iColorBandLegend $\to$ Legending`LegendDump`parseColorBand) you can find the following code Switch[Legending`LegendDump`colorfunction, _String, ...


8

After a rather long debugging session in our chat we could determine the reason of the problem and come up with a workaround. In short, we first tried whether the issue appears for the most basic Graphics[], which it didn't. As it turned out the gray background is introduced by using PlotLegends as in the example above. We went further by comparing ...


7

You can generate the legends separately with version 9's legends, but you'll have to manually input the colors (i.e. Automatic won't work): With[{cols = {Red, Blue}}, Column[{ GraphicsGrid[ Partition[ Table[ ListPlot[{Sqrt[Range[50]], Power[Range[50], (3)^-1]}, PlotStyle -> cols], ...


7

Plot[{Sin[x], Cos[x]}, {x, 0, 5}, PlotStyle -> {Blue, Red}, PlotLegends -> Placed[Framed@LineLegend[{Blue, Red}, {"L1", "L2"}, LegendLayout -> "Row"], Below]]


7

You can do : LogLinearPlot[{Log[x], x Sin[x], x Cos[x]}, {x, 1, 100}, PlotLegends -> Placed[{"Log(x)", None, "x Cos(x)"}, After]]


6

Plot[Sin[x], {x, 0, 1}, PlotLegends -> Placed[BarLegend[{ColorData["TemperatureMap"][1 - #] &, {15, 30}}, LegendMarkerSize -> 300, LegendLabel -> "T(\[Degree]C)", Ticks -> Table[{i, 45 - i}, {i, 15, 31}], LabelStyle -> {Bold, Black, 11}], {Top, Center}]]


6

In version 10 the PointSize of the legend will automatically match the PointSize of the Plot: ListPlot[Table[RandomReal[NormalDistribution[], {20, 2}], {2}], PlotLegends -> {"a", "b"}, PlotStyle -> PointSize[0.02]] Edit: The answer by @eldo made me realize, that this is only true up to a PointSize that is equal to the default ...


6

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


6

I always do it like this: cm = 72/2.54; BarChart[{{1, 2, 3}, {1, 3, 2}}, ChartLegends -> SwatchLegend[{"a", "b", "c"}, LegendMarkerSize -> 20], ImageSize -> 4 cm] top: code from your question, bottom: this code The colors of the legend are inherited from the BarChart object: BarChart[{{1, 2, 3}, {1, 3, 2}}, ChartStyle -> {Red, Green, ...


6

For ChartLegends, although the documentation does not mention any values other than "Row" and "Column" for the option LegendAppearance, it turns out you can also use this option to set the LegendMarkerSize BarChart[{{1, 2, 3}, {1, 3, 2}}, ChartStyle -> {Red, Green, Blue}, LegendAppearance -> {LegendMarkerSize -> 20}, ChartLegends -> {"a", ...


6

The following works in both v9 and v10: style = Directive[Thick, Black]; ContourPlot[y - x^2, {x, 0, 1}, {y, 0, 1}, ContourStyle -> Directive[Thick, Black, Opacity[1]], FrameStyle -> style, PlotLegends -> BarLegend[Automatic, Method -> {FrameStyle -> style}]] The idea to use the (undocumented) Method option comes from inspecting the ...


6

A reasonable workaround is to use SwatchLegend with LegendMarkers set: ListPlot[ Table[f, {f, {Sin[x], Cos[x]}}, {x, 0, 2 π, 0.1}], PlotLegends -> SwatchLegend[{"sin(x)", "cos(x)"}, LabelStyle -> 40, LegendMarkers -> "Bubble" ] ]


5

A few minor errors corrected (mostly extra commas): MakeExponent[x_Integer] := "\*SuperscriptBox[10," <> ToString[x] <> "]" tick = {10^#, MakeExponent[#]} & /@ Range[-10, 10, 1]; A = {1/#, #^2} & /@ Range[100]; B = {1/#, #^1.5 - 1} & /@ Range[100]; A1 = {1/#, #^2} & /@ Range[100]; B1 = {1/#, #^1.5 - 1} & /@ Range[100]; ...


5

The style for labels in a legend is determined by the option LabelStyle. To get the legend markers to match the style of the plot without rescaling, one approach is to use AbsoluteDashing and AbsoluteThickness to style the plot lines. With[{d = AbsoluteDashing[8], t = AbsoluteThickness[4]}, Plot[{E^(4 x), E^(3 x), E^(2 x), E^x, E^(x/2), E^(x/3), E^(x/4)}, ...


5

I am continuing to explore this as I can't yet demonstrate exactly what is going on, but first note that ArrayPlot does not experience this problem: tab = Table[Exp[-((i - 25)^2 + (j - 25)^2)], {i, 50}, {j, 50}]; ArrayPlot[500 * tab, PlotLegends -> Automatic, ColorFunction -> "ThermometerColors"] Note that I multiplied the original table by 500 ...


5

Adapting an example in the docs you could do it this way: table[pairs_] := Grid[pairs, BaseStyle -> {TextAlignment -> Left}, Alignment -> {Left, Automatic}] Then use LegendLayout -> table, to give you: Edit To reverse it just use Reverse: reversetable[pairs_] := Grid[Reverse@pairs, BaseStyle -> {TextAlignment -> Left}, ...


5

This is straightforward. All of the legend functions accept the generic, top level arguments, e.g. LineLegend[Automatic] which allows you to modify the behavior. So, you would use BarLegend[Automatic, LabelStyle -> {FontSize -> 20}].


5

I was able to isolate the problem with BarLegend in v.10.0.0. Yes, it is clearly a bug. Let us see the how the thin grey lines are implemented: Cases[ ToBoxes[BarLegend[{"DeepSeaColors", {0, 1}}, LegendLayout -> "ReversedColumn"]], _LineBox, Infinity] {LineBox[ NCache[{{-(15/2), 225/2}, {15/2, 225/2}, {15/ 2, -(225/2)}, {-(15/2), ...


4

Specifying FrameTicks adds a "legend" to MatrixPlot's row: legend = {"Hello", "How are you?", "Good Bye"}; MatrixPlot[{{1, 2, 1}, {3, 0, 1}, {0, 0, -1}}, FrameTicks -> { {True (* Left *), Thread[{Range@Length@legend, legend}](* Right *)}, {True (* Bottom *), False (* Top *)}}]


4

I don't know if there's an elegant way. The colors are embedded in the plots, and changing them after the fact takes some work. I basically do what the OP alluded to, but as postprocessing. Collect the colors in the graph and remap them according to some color function. plots = Table[ Plot[Evaluate[Table[Sin[(4(3-j)-i)x], {i, 4-j}]], {x, 0, 2 Pi}], ...


4

If you want legends, you may want to try the code I posted under Creating legends for plots with multiple lines?. To use it, copy all the definitions in the first code block of that answer, then re-create your plot this way: z[r_, t_] = {r^2 Cos[2 t] - 2 r Sin[t], 2 r Cos[t] + r^2 Sin[2 t]}; zz[t_] = Table[z[r, t], {r, 0.1, 4, 0.5}]; p = ...



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