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.

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
94 votes
9 answers
48k views

Making a Legend/Key in GraphViz

I’d like to include a legend or key in my GraphViz diagram. I’m having trouble figuring out what code to use, though. I also want to put it in a corner, but the only coord I know for sure is the ...
Synetech's user avatar
  • 9,853
88 votes
6 answers
111k views

Setting a fixed size for points in legend

I'm making some scatter plots and I want to set the size of the points in the legend to a fixed, equal value. Right now I have this: import matplotlib.pyplot as plt import numpy as np def rand_data(...
Gabriel's user avatar
  • 42.1k
88 votes
4 answers
149k views

creating over 20 unique legend colors using matplotlib

I am plotting 20 different lines on a single plot using matplotlib. I use a for loop for plotting and label every line with its key and then use the legend function for key in dict.keys(): plot(x,...
msohail's user avatar
  • 1,115
88 votes
4 answers
68k views

increase the linewidth of the legend lines in matplotlib

I know that if I change the linewidth of a line, that is automatically updated in the legend. However I would like to just change the legend linewidth without affecting the plot.
Andrea Zonca's user avatar
  • 8,693
86 votes
8 answers
47k views

Set legend symbol opacity

I'm working on a plot with translucent 'x' markers (20% alpha). How do I make the marker appear at 100% opacity in the legend? import matplotlib.pyplot as plt plt.plot_date( x = xaxis, y = yaxis, ...
Don Albrecht's user avatar
  • 1,282
79 votes
9 answers
148k views

How to set font size of Matplotlib axis Legend?

I have a code like this: import matplotlib.pyplot as plt from matplotlib.pyplot import * from matplotlib.font_manager import FontProperties fontP = FontProperties() fontP.set_size('xx-small') fig=...
Tapajit Dey's user avatar
  • 1,376
79 votes
1 answer
39k views

Control ggplot2 legend look without affecting the plot

I'm plotting lines with ggplot2 like this: ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line() + theme_bw() . I find legend marks to be small so I want them to be bigger. If I ...
baltazar's user avatar
  • 1,679
78 votes
4 answers
138k views

Change main plot legend label text

So far I have been able to label the subplots just fine but I'm having an issue with the main one. Here's the relevant part of my code: data_BS_P = data[channels[0]] data_BS_R = data[channels[1]] ...
N.K.'s user avatar
  • 813
78 votes
9 answers
158k views

How do I assign multiple legend labels at once?

I have the following dataset: x = [0, 1, 2, 3, 4] y = [ [0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [9, 8, 7, 6, 5] ] Now I plot it with: import matplotlib.pyplot as plt plt.plot(x, y) However, ...
Kit's user avatar
  • 31.2k
74 votes
4 answers
184k views

Remove line through legend marker with .plot

I have a matplotlib plot generated with the following code: import matplotlib.pyplot as pyplot Fig, ax = pyplot.subplots() for i, (mark, color) in enumerate(zip( ['s', 'o', 'D', 'v'], ['r', 'g', ...
jlconlin's user avatar
  • 14.8k
70 votes
2 answers
172k views

plot legends without border and with white background

I have a legend in a plot with a line (from an abline-statement) going through it. How can I achieve that the abline gets invisible in proximity of the legend? This should be achievable by setting the ...
biotom's user avatar
  • 751
68 votes
1 answer
49k views

R ggplot2 legend inside the figure

So, I have the following data.frame, and I want to generate two plots in one graph for yval vs. xval, for each zval and type tp. The lef > df xval yval se zval cond 1 1.0 ...
Vahid Mirjalili's user avatar
66 votes
3 answers
69k views

Styling part of label in legend in matplotlib

Is it possible to have part of the text of a legend in a particular style, let's say, bold or italic?
englebip's user avatar
  • 1,025
65 votes
6 answers
75k views

How can I remove the legend title in ggplot2?

I have a question concerning the legend in ggplot2. Say I have a hypothetical dataset about mean carrot length for two different colours at two farms: carrots<-NULL carrots$Farm<-rep(c("X","Y")...
susjoh's user avatar
  • 2,388
63 votes
5 answers
56k views

Reverse the order of a legend

I use the following code to plot the bar graph and need to present a legend in reverse order. How can I do it? colorsArr = plt.cm.BuPu(np.linspace(0, 0.5, len(C2))) p = numpy.empty(len(C2), dtype=...
YAKOVM's user avatar
  • 10.1k
61 votes
4 answers
106k views

Is it possible to add a string as a legend item

I am producing some plots in matplotlib and would like to add explanatory text for some of the data. I want to have a string inside my legend as a separate legend item above the '0-10' item. Does ...
Osmond Bishop's user avatar
59 votes
7 answers
80k views

Which CSS tag creates a box like this with title?

I want to create a box like this with title: Can any one please let me know if there is a default CSS tag to do this? Or do I need to create my custom style?
Mozammel's user avatar
  • 1,051
58 votes
1 answer
47k views

ggplot2 legend items in a single horizontal row

It seems that the code I wrote and tested last week has now suddenly decided to change the output even though nothing in the code or the version of R or Rstudio has changed. Previously when I drew a ...
Psychologeek's user avatar
57 votes
3 answers
93k views

How to change the legend edgecolor and facecolor

Is there while rcParams['legend.frameon'] = 'False' a simple way to fill the legend area background with a given colour. More specifically I would like the grid not to be seen on the legend area ...
user1850133's user avatar
  • 2,923
56 votes
7 answers
200k views

No handles with labels found to put in legend

I'm trying to create a parallelogram in PyPlot. I'm not up to drawing the parallelogram--first I'm putting in the vector arrows--using the following code: fig = plt.figure() ax = fig.add_subplot(111) ...
Yehuda's user avatar
  • 1,883

15 30 50 per page
1
2 3 4 5
104