legend

Matplotlib - global legend and title aside subplots

这一生的挚爱 提交于 2019-11-26 15:24:52
问题 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",

matplotlib: colorbars and its text labels

感情迁移 提交于 2019-11-26 15:20:55
问题 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 matplotlib.colors import ListedColormap #discrete color scheme cMap = ListedColormap(['white', 'green', 'blue','red']) #data np.random.seed(42) data = np.random.rand(4, 4) fig, ax = plt.subplots() heatmap = ax.pcolor(data, cmap=cMap) #legend cbar = plt.colorbar(heatmap) cbar.ax.set_yticklabels([

How do I position two legends independently in ggplot

◇◆丶佛笑我妖孽 提交于 2019-11-26 15:20:20
Title pretty well covers it. I have two legends, relating to size and colour, and wish to have one,say, on the top and one within the graph. Is this possible and, if so, how TIA From my understanding, basically there is very limited control over legends in ggplot2 . Here is a paragraph from the Hadley's book (page 111): ggplot2 tries to use the smallest possible number of legends that accurately conveys the aesthetics used in the plot. It does this by combining legends if a variable is used with more than one aesthetic. Figure 6.14 shows an example of this for the points geom: if both colour

Remove the legend on a matplotlib figure

混江龙づ霸主 提交于 2019-11-26 15:14:50
问题 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 leaves an ugly white rectangle in the upper right corner.) 回答1: As of matplotlib v1.4.0rc4, a remove method has been added to the legend object. Usage: ax.get_legend().remove() or legend = ax.legend(...) ... legend.remove() See here for the commit where this was introduced. 回答2: You could use the

Combine two Pyplot patches for legend

Deadly 提交于 2019-11-26 14:38:07
问题 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,

Match legend text color in geom_text to symbol

£可爱£侵袭症+ 提交于 2019-11-26 14:34:42
问题 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

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

南笙酒味 提交于 2019-11-26 14:14:06
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 very close together and I am wondering how to space them farther apart. I think the best option is to use guide_legend within guides : p + guides(fill=guide_legend( keywidth=0.1, keyheight=0.1, default.unit="inch") ) Note the use of default.unit , no need to load grid package. ggplot2 v3.0.0 released in July 2018 has working options to modify legend.spacing.x , legend.spacing.y and legend.text .

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

故事扮演 提交于 2019-11-26 12:38:05
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 being able to fiddle with the exact shrinking of the axis so that the legend fits. Shrinking the axes, however, is not an ideal solution because it makes the data smaller making it actually more difficult to interpret; particularly when its complex and there are lots of things going on ... hence needing a large legend The example of a complex legend in the documentation demonstrates the need for this

Add legend to scatter plot (PCA)

半腔热情 提交于 2019-11-26 12:32:50
I am a newbie with python and found this excellent PCA biplot suggestion ( Plot PCA loadings and loading in biplot in sklearn (like R's autoplot) ). Now I tried to add a legend to the plot for the different targets. But the command plt.legend() doesn't work. Is there an easy way to do it? As an example, the iris data with the biplot code from the link above. import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.decomposition import PCA import pandas as pd from sklearn.preprocessing import StandardScaler iris = datasets.load_iris() X = iris.data y = iris

How to display legend for Pie Chart in columns?

家住魔仙堡 提交于 2019-11-26 12:32:47
I have a PieChart with many sections, legend for this PieChart renders as one row. How to render legend as two columns? trashgod The method getLegendItem() , seen here , provides all the information needed to render a legend item in any Container you choose. GridLayout(0, 2) will arrange them in two columns for any number of rows. To suppress the existing legend, set legend to false when you call your chart factory; the items will still be available, as suggested here . Addendum: Based on PieChartDemo1 , this fragment uses the getLegendItems().iterator and a variation of this ColorIcon .