我对ggplot2中的传说有疑问.
假设我有一个关于两个农场两种不同颜色的平均胡萝卜长度的假设数据集:
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)
Run Code Online (Sandbox Code Playgroud)
我做了一个简单的条形图:
require(ggplot2)
p<-ggplot(carrots,aes(y=MeanLength,x=Farm,fill=Type)) +
geom_bar(position="dodge") +
opts(legend.position="top")
p
Run Code Online (Sandbox Code Playgroud)
我的问题是:有没有办法从图例中删除标题('类型')?
谢谢!
这是一个非常简单的例子:
xvalues = [2,3,4,6]
for x in xvalues:
plt.axvline(x,color='b',label='xvalues')
plt.legend()
Run Code Online (Sandbox Code Playgroud)
现在,图例将在图例中将'xvalues'显示为蓝线4次.有没有比以下更优雅的方法来解决这个问题?
for i,x in enumerate(xvalues):
if not i:
plt.axvline(x,color='b',label='xvalues')
else:
plt.axvline(x,color='b')
Run Code Online (Sandbox Code Playgroud) Rdates <- c("2007-01-31","2007-02-28","2007-03-30","2007-04-30","2007-05-31","2007-06-29","2007-07-31","2007-08-31","2007-09-28","2007-10-31")
Rdates <- as.Date(Rdates)
Cnames <- c("Column 1 Really Long","Column 2 Really Long","Column 3 Really Long","Column 4 Really Long","Column 5 Really Long","Column 6 Really Long","Column 7 Really Long","Column 8 Really Long","Column 9 Really Long","Column 10 Really Long")
MAINDF <- data.frame(replicate(10,runif(10,-0.03,0.03)))
rownames(MAINDF) <- Rdates
colnames(MAINDF) <- Cnames
CUSTOMpalette <- c("#1a2ffa", "#0d177d", "#1a9ffa", "#fa751a", "#4b8e12", "#6fd21b", "#fae51a", "#c3b104", "#f5df05", "#dcc805")
MAINDF[,"dates"] <- Rdates
MAINDF <- melt(MAINDF,id.vars="dates")
gg <- ggplot(MAINDF, aes(x = dates, y = value, fill = variable))
gg <- …
Run Code Online (Sandbox Code Playgroud) 我知道如果我改变一条线的线宽,它会在图例中自动更新.但是我想在不影响绘图的情况下更改图例线宽.
我在一个情节中有一个传说,一条线(来自一个abline-statement)经过它.如何在传奇附近看到abline隐形?这应该可以通过将图例背景设置为白色而没有边框来实现,但是我该如何实现呢?假设图形应如下所示:
windows.options(width=30, height=12)
plot(1:10)
abline(v=seq(1,10,1), col='grey', lty='dotted')
legend(4.8, 3, "This legend text should not be disturbed by the dotted grey lines")
Run Code Online (Sandbox Code Playgroud)
并且让它变得更复杂:如果图例干扰了点图的点:我怎样才能实现在图例附近看到的ablines不可见(如上所示),但点仍然可见?
windows.options(width=30, height=12)
plot(1:10)
abline(v=seq(1,10,1), col='grey', lty='dotted')
legend(1, 5, "This legend text should not be disturbed by the dotted grey lines, but the plotted dots should still be visible")
Run Code Online (Sandbox Code Playgroud)
最后:有没有办法在图例语句中引入换行符?
我创建了一个4D散点图来表示特定区域的不同温度.创建图例时,图例会显示正确的符号和颜色,但会在其中添加一条线.我正在使用的代码是:
colors=['b', 'c', 'y', 'm', 'r']
lo = plt.Line2D(range(10), range(10), marker='x', color=colors[0])
ll = plt.Line2D(range(10), range(10), marker='o', color=colors[0])
l = plt.Line2D(range(10), range(10), marker='o',color=colors[1])
a = plt.Line2D(range(10), range(10), marker='o',color=colors[2])
h = plt.Line2D(range(10), range(10), marker='o',color=colors[3])
hh = plt.Line2D(range(10), range(10), marker='o',color=colors[4])
ho = plt.Line2D(range(10), range(10), marker='x', color=colors[4])
plt.legend((lo,ll,l,a, h, hh, ho),('Low Outlier', 'LoLo','Lo', 'Average', 'Hi', 'HiHi', 'High Outlier'),numpoints=1, loc='lower left', ncol=3, fontsize=8)
Run Code Online (Sandbox Code Playgroud)
我试过换Line2D
到Scatter
和scatter
.Scatter
返回错误并scatter
更改了图表并返回了错误.
有了scatter
,我改为range(10)
包含数据点的列表.每个列表包含x,y或z变量.
lo = plt.scatter(xLOutlier, yLOutlier, zLOutlier, …
Run Code Online (Sandbox Code Playgroud) 我有以下数据(temp.dat
请参阅完整数据的结束注释)
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 5.142464
...
Run Code Online (Sandbox Code Playgroud)
我可以生成以下图表:
ggplot(temp.dat) +
geom_line(aes(x = Year, y = Capex, group = State, colour = State))
Run Code Online (Sandbox Code Playgroud)
而不是传说,我喜欢标签
我在以下链接的答案中注意到了baptiste的评论,但是当我尝试调整他的代码(geom_text(aes(label = State, colour = State, x = Inf, y = Capex), hjust = -1)
)时,文本没有出现.
temp.dat <- structure(list(Year = c("2003", "2004", "2005", "2006", "2007",
"2008", "2009", "2010", "2011", "2012", "2013", …
Run Code Online (Sandbox Code Playgroud) 我有这样的代码:
import matplotlib.pyplot as plt
from matplotlib.pyplot import *
from matplotlib.font_manager import FontProperties
fontP = FontProperties()
fontP.set_size('xx-small')
fig=plt.figure()
ax1=fig.add_subplot(111)
plot([1,2,3], label="test1")
ax1.legend(loc=0, ncol=1, bbox_to_anchor=(0, 0, 1, 1),
prop = fontP,fancybox=True,shadow=False,title='LEGEND')
plt.show()
Run Code Online (Sandbox Code Playgroud)
从图中可以看出,Fontsize中的设置不会影响Legend Title字体大小.
如何将图例标题的字体大小设置为较小的尺寸?
我不得不重新订购传奇中的物品,当时我不认为我应该这样做.我尝试:
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()
Run Code Online (Sandbox Code Playgroud)
最终以中上方为底.我怎样才能得到正确的订单?它不是由创建顺序决定的吗?
更新:以下内容可用于强制执行订单.我认为这可能是最简单的方法,这看起来很尴尬.问题是什么决定了原始订单?
hh=[ht,h1,hb]
legend([ht,h1.patches[0],hb],[H.get_label() for H in hh])
Run Code Online (Sandbox Code Playgroud) 我有以下数据集:
x = [0, 1, 2, 3, 4]
y = [ [0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[9, 8, 7, 6, 5] ]
Run Code Online (Sandbox Code Playgroud)
现在我绘制它:
import matplotlib.pyplot as plt
plt.plot(x, y)
Run Code Online (Sandbox Code Playgroud)
但是,我想用这个命令标记3个y数据集,这会在.legend()
调用时引发错误:
lineObjects = plt.plot(x, y, label=['foo', 'bar', 'baz'])
plt.legend()
File "./plot_nmos.py", line 33, in <module>
plt.legend()
...
AttributeError: 'list' object has no attribute 'startswith'
Run Code Online (Sandbox Code Playgroud)
当我检查lineObjects
:
>>> lineObjects[0].get_label()
['foo', 'bar', 'baz']
>>> lineObjects[1].get_label()
['foo', 'bar', 'baz']
>>> lineObjects[2].get_label()
['foo', 'bar', 'baz']
Run Code Online (Sandbox Code Playgroud)
是否有一种优雅的方法来通过使用 …
legend ×10
matplotlib ×6
python ×4
r ×4
ggplot2 ×3
font-size ×1
label ×1
plot ×1
scatter-plot ×1