Skip to main content
The 2024 Developer Survey results are live! See the results

Questions tagged [legend]

A graph legend is commonly used when plotting several sets of data within the same diagram. The legend associates each plot with its corresponding data.

legend
1661 votes
18 answers
1.9m views

How to put the legend outside the plot

I have a series of 20 plots (not subplots) to be made in a single figure. I want the legend to be outside of the box. At the same time, I do not want to change the axes, as the size of the figure gets ...
pottigopi's user avatar
  • 16.6k
519 votes
9 answers
1.0m views

How to change legend fontsize with matplotlib.pyplot

Simple question here: I'm trying to get the size of my legend using matplotlib.pyplot to be smaller (i.e., the text to be smaller). The code I'm using goes something like this: plot.figure() plot....
mike's user avatar
  • 23.5k
508 votes
7 answers
1.4m views

Adding a matplotlib legend

How can one create a legend for a line graph in Matplotlib's PyPlot without creating any extra variables? Please consider the graphing script below: if __name__ == '__main__': PyPlot.plot(length, ...
Nafiul Islam's user avatar
  • 81.8k
481 votes
11 answers
481k views

Secondary axis with twinx(): how to add to legend

I have a plot with two y-axes, using twinx(). I also give labels to the lines, and want to show them with legend(), but I only succeed to get the labels of one axis in the legend: import numpy as np ...
joris's user avatar
  • 138k
376 votes
4 answers
604k views

Remove legend ggplot 2.2

I'm trying to keep the legend of one layer (smooth) and remove the legend of the other (point). I have tried shutting off the legends with guides(colour = FALSE) and geom_point(aes(color = vs), show....
Guy's user avatar
  • 3,785
337 votes
10 answers
378k views

How do I make a single legend for many subplots?

I am plotting the same type of information, but for different countries, with multiple subplots with Matplotlib. That is, I have nine plots on a 3x3 grid, all with the same for lines (of course, ...
pocketfullofcheese's user avatar
330 votes
11 answers
444k views

Remove the legend on a matplotlib figure

To add a legend to a matplotlib plot, one simply runs legend(). How to remove a legend from a plot? (The closest I came to this is to run legend([]) in order to empty the legend from data. But that ...
Olivier Verdier's user avatar
298 votes
7 answers
254k views

Moving matplotlib legend outside of the axis makes it cutoff by the figure box

I'm familiar with the following questions: Matplotlib savefig with a legend outside the plot How to put the legend out of the plot It seems that the answers in these questions have the luxury of ...
jonathanbsyd's user avatar
  • 8,341
222 votes
11 answers
452k views

Plot a legend outside of the plotting area in base graphics?

As the title says: How can I plot a legend outside the plotting area when using base graphics? I thought about fiddling around with layout and produce an empty plot to only contain the legend, but I ...
Henrik's user avatar
  • 14.4k
212 votes
9 answers
271k views

Add a common Legend for combined ggplots

I have two ggplots which I align horizontally with grid.arrange. I have looked through a lot of forum posts, but everything I try seem to be commands that are now updated and named something else. ...
jO.'s user avatar
  • 3,472
200 votes
8 answers
73k views

Remove 'a' from legend when using aesthetics and geom_text

How can I can remove the letter 'a' from the legend generated by this code? If I remove the geom_text, then the 'a' letter will not show in the legend. I want to keep geom_text, though. ggplot(data = ...
user2700207's user avatar
  • 2,043
187 votes
9 answers
194k views

Is there a way to change the spacing between legend items in ggplot2?

Is there a way to change the spacing between legend items in ggplot2? I currently have legend.position ="top" which automatically produces a horizontal legend. However, the spacing of the items is ...
overflowname's user avatar
  • 2,153
184 votes
4 answers
406k views

Add legend to ggplot2 line plot

I have a question about legends in ggplot2. I managed to plot three lines in the same graph and want to add a legend with the three colors used. This is the code used library(ggplot2) ## edit from ...
pacomet's user avatar
  • 5,111
173 votes
5 answers
239k views

How to manually create a legend

I am using matplotlib and I would like to manually add items to the legend that are a color and a label. I am adding data to to the plot to specifying there would lead to a lot of duplicates. My ...
Brady forcier's user avatar
173 votes
4 answers
187k views

Global legend and title aside subplots

I've started with matplot and managed some basic plots, but now I find it hard to discover how to do some stuff I need now :( My actual question is how to place a global title and global legend on a ...
Gere's user avatar
  • 12.5k
168 votes
4 answers
538k views

matplotlib: colorbars and its text labels

I'd like to create a colorbar legend for a heatmap, such that the labels are in the center of each discrete color. Example borrowed from here: import matplotlib.pyplot as plt import numpy as np from ...
dimka's user avatar
  • 4,459
133 votes
4 answers
151k views

Remove extra legends in ggplot2

I have a simple data frame that I'm trying to do a combined line and point plot using ggplot2. Supposing my data looks like this: df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20), ...
Wilduck's user avatar
  • 14k
130 votes
4 answers
56k views

Keep unused levels in bar plot

I want to plot unused levels (that is, levels where the count is 0) in my bar-plot, however, unused levels are dropped and I cannot figure out how to keep them df <- data.frame(type=c("A", "A", "A"...
Ulrik's user avatar
  • 1,625
123 votes
2 answers
145k views

Legend on bottom, two rows wrapped in ggplot2 in r

library(ggplot2) library(scales) library(reshape2) Rdates <- c("2007-01-31","2007-02-28","2007-03-30","2007-04-30","2007-05-31","2007-06-29&...
Trevor Nederlof's user avatar
119 votes
6 answers
94k views

Stop matplotlib repeating labels in legend

Here is a very simplified example: xvalues = [2,3,4,6] for x in xvalues: plt.axvline(x,color='b',label='xvalues') plt.legend() The legend will now show 'xvalues' as a blue line 4 times in the ...
Anake's user avatar
  • 7,549
118 votes
6 answers
126k views

Show only certain items in legend

I currently am plotting a stacked bar graph of a large amount of taxonomic data, and only wish to show significant species in the legend (out of ~500 I wish to show ~25). Is there a simple way to do ...
Funsaized's user avatar
  • 2,078
116 votes
10 answers
164k views

Plot labels at ends of lines

I have the following data (temp.dat see end note for full data) Year State Capex 1 2003 VIC 5.356415 2 2004 VIC 5.765232 3 2005 VIC 5.247276 4 2006 VIC 5.579882 5 2007 VIC ...
Hugh's user avatar
  • 15.9k
116 votes
7 answers
129k views

How is order of items in matplotlib legend determined?

I am having to reorder items in a legend, when I don't think I should have to. I try: from pylab import * clf() ax=gca() ht=ax.add_patch(Rectangle((1,1),1,1,color='r',label='Top',alpha=.1)) h1=ax.bar(...
CPBL's user avatar
  • 3,940
114 votes
4 answers
340k views

How to edit a seaborn legend title and labels for figure-level functions

I've created this plot using Seaborn and a pandas dataframe (data): My code: import seaborn as sns g = sns.lmplot('credibility', 'percentWatched', data=data, hue='millennial', markers=["+",...
Oliver G's user avatar
  • 1,171
114 votes
4 answers
150k views

How to move or position a legend in ggplot2

I'm trying to create a ggplot2 plot with the legend beneath the plot. The ggplot2 book says on p 112 "The position and justification of legends are controlled by the theme setting legend.position, ...
Dan Goldstein's user avatar
109 votes
4 answers
182k views

How to place two different legends on the same graph

I have a plot where different colors are used for different parameters, and where different line styles are used for different algorithms. The goal is to compare the results of the different ...
Charles Brunet's user avatar
108 votes
5 answers
371k views

How to specify legend position in graph coordinates

I am aware of the bbox_to_anchor keyword and this thread, which very helpfully suggests how to manually place the legend: How to put the legend out of the plot However, I'd like to use the ...
mzzx's user avatar
  • 2,104
100 votes
8 answers
211k views

Move seaborn plot legend to a different position

I'm using factorplot(kind="bar") with seaborn. The plot is fine except the legend is misplaced: too much to the right, text goes out of the plot's shaded area. How do I make seaborn place the legend ...
user124114's user avatar
  • 8,548
96 votes
5 answers
374k views

Matplotlib scatter plot legend

I created a 4D scatter plot graph to represent different temperatures in a specific area. When I create the legend, the legend shows the correct symbol and color but adds a line through it. The code I'...
user2386081's user avatar
  • 1,097
96 votes
5 answers
164k views

HighCharts Hide Series Name from the Legend

I try to solve this problem several times and give up. Now, when I have met him again, I decided to ask for some help. I have this code for my Legend: legend: { layout: 'vertical', align: '...
gotqn's user avatar
  • 43.4k

15 30 50 per page
1
2 3 4 5
173