legend

How can I remove the legend title in ggplot2?

情到浓时终转凉″ 提交于 2019-11-27 11:25:47
问题 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"),2) carrots$Type<-rep(c("Orange","Purple"),each=2) carrots$MeanLength<-c(10,6,4,2) carrots<-data.frame(carrots) I make a simple bar plot: require(ggplot2) p<-ggplot(carrots,aes(y=MeanLength,x=Farm,fill=Type)) + geom_bar(position="dodge") + opts(legend.position="top") p My question is: is there a way to

Matplotlib - global legend and title aside subplots

只愿长相守 提交于 2019-11-27 11:16:11
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 figure with subplots. I'm doing 2x3 subplots where I have a lot of different graphs in various colors (about 200). To distinguish (most) of them I wrote something like def style(i, total): return dict(color=jet(i/total), linestyle=["-", "--", "-.", ":"][i%4], marker=["+", "*", "1", "2", "3", "4", "s"][i%7]) fig=plt.figure() p0=fig.add_subplot(321) for i, y in enumerate(data): p0.plot(x, trans0(y),

How is order of items in matplotlib legend determined?

妖精的绣舞 提交于 2019-11-27 10:06:53
问题 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=.01)) h1=ax.bar(1,2,label='Middle') hb=ax.add_patch(Rectangle((1,1),1,1,color='k',label='Bottom',alpha=.01)) legend() show() and end up with Bottom above Middle. How can I get the right order? Is it not determined by creation order? Update: The following can be used to force the order. I think this may be the

Combine two Pyplot patches for legend

和自甴很熟 提交于 2019-11-27 09:19:25
I am trying to plot some data with confidence bands. I am doing this with two plots for each data stream: plot , and fill_between . I would like the legend to look similar to the plots, where each entry has a box (the color of the confidence region) with a darker, solid line passing through the center. So far I have been able to use patches to create the rectangle legend key, but I don't know how to achieve the centerline. I tried using hatch, but there is no control over the placement, thickness, or color. My original idea was to try and combine two patches (Patch and 2DLine); however, it

Match legend text color in geom_text to symbol

▼魔方 西西 提交于 2019-11-27 09:05:41
I am trying to color match the text of the legend to the color of text produced by a factored variable using geom_text . Here is a minimal working example: df <- data.frame(a=rnorm(10),b=1:10,c=letters[1:10],d=c("one","two")) p1 <-ggplot(data=df,aes(x=b,y=a)) p1 <- p1 + geom_text(aes(label = c, color=d, fontface="bold")) p1 <- p1 + scale_color_hue(name="colors should match",breaks=c("one", "two"), labels=c("should be pink", "should be blue")) p1 I am sure its a simple fix. Any suggestions or reference to prior posts would help. I did not find anything specific to this. Following on from joran

Make a rectangular legend, with rows and columns labeled, in grid

喜你入骨 提交于 2019-11-27 08:23:42
I've got a ggplot where I'm mapping factors to both fill and alpha, like this: set.seed(47) the_data <- data.frame(value = rpois(6, lambda=20), cat1 = rep(c("A", "B"), each = 3), cat2 = rep(c("X", "Y", "Z"), 2)) ggplot(the_data, aes(y = value, x = cat2, alpha = cat1, fill = cat2)) + geom_bar(stat = "identity", position = "dodge") + scale_alpha_discrete(range = c(0.5, 1)) + theme_bw() The people I'm producing it for don't find the legend for alpha very clear. I think a good alternative would be something like this (which I hacked together in base graphics): I know I can't generate a legend like

Is it possible to add a string as a legend item in matplotlib

我们两清 提交于 2019-11-27 08:01:14
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 anyone know if there is a possible way to do this? This is the code for my legend: ax.legend(['0-10','10-100','100-500','500+'],loc='best') Sure. ax.legend() has a two argument form that accepts a list of objects (handles) and a list of strings (labels). Use a dummy object (aka a "proxy artist" ) for your extra string. I picked a matplotlib.patches.Rectangle with no fill and 0 linewdith below, but you

Stop matplotlib repeating labels in legend

我只是一个虾纸丫 提交于 2019-11-27 06:52:32
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 legend. Is there a more elegant way of fixing this than the following? for i,x in enumerate(xvalues): if not i: plt.axvline(x,color='b',label='xvalues') else: plt.axvline(x,color='b') ecatmur legend takes as arguments a list of axis handles and labels, defaulting to plt.gca().get_legend_handles_labels() . You can remove duplicate labels when calling legend , for example: from collections import

How can I customize the positions of legend elements?

雨燕双飞 提交于 2019-11-27 06:47:30
问题 I have a figure with several plots and a legend. Is it possible to change the arrangement of the elements in the legend manually? For example, how could you make a two-column legend? 回答1: Of course this is possible. See an explanation here: http://undocumentedmatlab.com/blog/multi-column-grid-legend/ 回答2: To make a two-column legend, the general consensus seems to be that you need to create two separate legends and manually place them side by side. Solution simplified from discussion here. x

Adjusting the width of legend for continuous variable

跟風遠走 提交于 2019-11-27 06:39:01
问题 I have a script below to illustrate my question: temp.df <- data.frame(x=1:100,y=1:100,z=1:100,stringsAsFactors=FALSE) chart <- ggplot(data=temp.df,aes(x=x,y=y)) chart <- chart + geom_line(aes(colour=z)) chart <- chart + scale_colour_continuous(low="blue",high="red") chart <- chart + theme(legend.position="bottom") # so far so good, but I think the legend positioned at bottom with such a small size is a waste of space, so I want to "widen" it using the line below... chart <- chart + guides