我知道如何使用 设置 matplotlib 图的图例位置plt.legend(loc='lower left')
,但是,我正在使用 Pandas 方法进行绘图df.plot()
并且需要将图例位置设置为“左下角”。
有谁知道怎么做?
编辑:我实际上是在寻找一种方法来通过 pandas'df.plot()
而不是通过plt.legend(loc='lower left')
下面是我试图解决的问题的可重现的例子.我已经在ggplot2中创建了各种各样的热图,事情进展顺利.由于我在geom_text上使用了数据的百分号,我想让geom_tile的图例也显示百分号(我现在只能将实际值乘以100).理想情况下,我希望右侧的图例栏显示8%,4%,0%, - 4%, - 8%.
#load in libraries
require(plyr)
require(dplyr)
require(reshape2)
require(ggplot2)
require(scales)
testDF <- structure(list(strategies = structure(c(8L, 7L, 6L, 5L, 4L, 3L,
2L, 1L), .Label = c("Class 1", "Class 2",
"Class 3", "Class 4", "Class 5", "Class 6",
"Class 7", "Class 8"), class = "factor"),
School1 = c(0.0355662887589396, 0.0316753241146625, 0.00606392341292672,
0.0250738342627283, -0.0405709181701368, 0.0237665074609996,
0.00587364885411765, -0.0343914002059331), School2 = c(NA, NA,
NA, 0.0225535750673764, NA, -0.00448947685878404, NA, -0.0446386763157662
), School3 = c(NA, NA, NA, 0.0261099462365593, NA, 0.0199735626692146,
NA, -0.0272279264519992), School4 = c(NA, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在图例中为代码文本着色.(因为我试图将几个图表分成不同的类别,我不能仅仅依赖图例中的线条颜色.)我已经设法为整个图例设置文本颜色,但我无法设法逐行分配.这可能吗?
代码到目前为止:
list={'Label 1','Label 2','Label 3'};
leg=legend(list);
set(leg,'Textcolor',[1 0 0])
Run Code Online (Sandbox Code Playgroud)
将整个图例的文本颜色设置为红色.我希望能够制作一些红色和一些黑色.我尝试将颜色数组指定为nx 3矩阵,但MATLAB并不那么喜欢.我也使用了传奇属性get(leg)
,但我找不到其他任何看似有用的东西.有什么建议?
考虑以下代码:
t=0:.01:(2*pi);
y=[sin(t);sin(t-pi/12);sin(t-pi/6);sin(t-pi/4)];
figure(1)
clf
subplot(6,1,5)
plot(t,y)
xlim([0 2*pi])
legend('1','2','3','4')
Run Code Online (Sandbox Code Playgroud)
它产生以下数字:
有没有办法将图例更改为2列布局?所以它会
--- 1 --- 3
--- 2 --- 4
代替
--- 1
--- 2
--- 3
--- 4
因此,图例边界线不会越过图形边界线.
我找到了gridLegend
脚本,但我更喜欢直接编写代码.
我无法正确设置图例的位置,我试图玩xjust
,ncol
但它仍然出现在图形的中间.
你能帮助它将它与边界对齐(左边或右边我不在乎)?
此外,是否可以在图例中画出较短的线条?这些看起来太长了.
par(mar=c(5,4,4,5)+.1)
plot(1:109,rnorm(109), type="l",col="blue", lwd=2,
main="Compare signal and std deviation MC3 ch44",
ylab="signal", xlab="locations")
par(new=TRUE)
plot(1:109, rnorm(109), ylab="",xlab="", type="l",
col="red",xaxt="n",yaxt="n", lwd=2)
axis(4)
mtext("std. deviation",side=4,line=3)
legend("topright",col=c("blue", "red"), lwd=2,
legend=c("signal","std dev"), cex=0.85, bty="n", xjust=1)
Run Code Online (Sandbox Code Playgroud) 考虑一下我的高图,就像这个带有传奇的高图一样
我想在图表之外的传奇.请帮忙.
注意:我希望我的图例属性相同.
legend{
layout: 'vertical',
align: 'right',
verticalAlign: 'middle'
}
Run Code Online (Sandbox Code Playgroud)
但是我希望它们在图表之外.请帮忙.
我有一个数据帧df
:
structure(list(y = c(2268.14043972082, 2147.62290922552, 2269.1387550775,
2247.31983098201, 1903.39138268307, 2174.78291538358, 2359.51909126411,
2488.39004804939, 212.851575751527, 461.398994384333, 567.150629704352,
781.775113821961, 918.303706148872, 1107.37695799186, 1160.80594193377,
1412.61328924168, 1689.48879626486, 260.737164468854, 306.72700499362,
283.410379620422, 366.813913489692, 387.570173754128, 388.602676983443,
477.858510450125, 128.198042456082, 535.519377609133, 1028.8780498564,
1098.54431357711, 1265.26965941035, 1129.58344809909, 820.922447928053,
749.343583476846, 779.678206156474, 646.575242339517, 733.953282899613,
461.156280127354, 906.813018662913, 798.186995701282, 831.365377249207,
764.519073183124, 672.076289062505, 669.879217186302, 1341.47673353751,
1401.44881976186, 1640.27575962036), P = c(1750.51986303926,
1614.11541634798, 951.847023338079, 1119.3682884872, 1112.38984390156,
1270.65773075982, 1234.72262170166, 1338.46096616983, 1198.95775346458,
1136.69287367165, 1265.46480803983, 1364.70149818063, 1112.37006707489,
1346.49240261316, 1740.56677791104, 1410.99217295647, 1693.18871380948,
901.760176040232, 763.971046562772, 994.8699095021, 972.755147593882,
1011.41669411398, 643.705302958842, 537.54384616157, 591.212003320456,
464.405641604215, 0, 0, …
Run Code Online (Sandbox Code Playgroud) 我想自定义我的柱形图API,如下所示
1)当我单击图例时,与其关联的数据集应返回null并以禁用的颜色显示图例.我的代码如下.
function drawVisualization() {
// Create and populate the data table.
var chart_div = document.getElementById('visualization2');
var data2 = google.visualization.arrayToDataTable([["Sections","Client Scored",{ role: "style" },"Client Confidence",{ role: "style" },"Average Mark",{ role: "style" },"Average Confidence",{ role: "style" }],["Set 1",90,"opacity: 1",95,"opacity: 0.5",78,"opacity: 1",69,"opacity: 0.5"],["Set 2",65,"opacity: 1",73,"opacity: 0.5",99,"opacity: 1",99,"opacity: 0.5"]]);
var options = {
title:"Understanding",
width:'100%', height:600,seriesType: "bars"
,series:{1: {type: "line",pointSize: 10,lineWidth :0},3: {type: "line",pointSize: 10,lineWidth :0}}
,colors: ['#fafe14','#fafe14','#05afed','#05afed']
,vAxis: {title: "%Score",format: '##', minValue: '1', maxValue: '8'},
hAxis: {title: "",slantedText: true,slantedTextAngle:60, maxTextLines: 5, maxAlternation: …
Run Code Online (Sandbox Code Playgroud) google-visualization bar-chart legend-properties column-chart
如果您使用 matplotlib 绘制多条线或点,有时您可能会发现重复标签的情况。例如:
for i in range(5):
Y1=boatarrays[i]
Y2=cararrays[i]
ax.plot(X,Y1,color='r',label='Boats')
ax.plot(X,Y2,color='b',label='Cars')
Run Code Online (Sandbox Code Playgroud)
如何只拥有'Boats'
且'Cars'
只出现一次?
seaborn.objects
我正在使用v0.12.0 中提供的新API。所有示例均未显示如何移动图例位置。例如,将图例移至底部。有没有办法做到这一点?
使用, 如本答案sns.move_legend
所示会产生.TypeError
import seaborn as sns
import seaborn.objects as so
healthexp = sns.load_dataset('healthexp')
p = so.Plot(healthexp, "Year", "Spending_USD", color="Country")
p.add(so.Line())
sns.move_legend(p, "upper left", bbox_to_anchor=(.55, .45), title='Species')
Run Code Online (Sandbox Code Playgroud)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [24], in <cell line: 3>()
1 p = so.Plot(healthexp, "Year", "Spending_USD", color="Country")
2 p.add(so.Line())
----> 3 sns.move_legend(p, "upper left", bbox_to_anchor=(.55, .45), title='Species')
File ~\anaconda3\envs\py10\lib\site-packages\seaborn\utils.py:444, in move_legend(obj, loc, **kwargs)
442 else:
443 err = "`obj` must …
Run Code Online (Sandbox Code Playgroud) legend ×5
plot ×3
r ×3
ggplot2 ×2
matlab ×2
matplotlib ×2
python ×2
bar-chart ×1
column-chart ×1
graph ×1
graphics ×1
highcharts ×1
javascript ×1
pandas ×1
seaborn ×1
textcolor ×1