标签: legend

如何在 matplotlib 中将图例的右上角锚定到轴的右上角?

我试图将 matplotlib 图的图例恰好定位在轴的右上角,以便图例的边缘和轴的边缘之间没有微小的线条部分。我想通过直接调用 Legend 对象的方法来完成此操作,例如Legend.set_bbox_to_anchor(),但我的尝试似乎根本没有移动图例。

这是我最近的尝试:

leg.set_bbox_to_anchor((1,1), transform = ax.transAxes)
Run Code Online (Sandbox Code Playgroud)

其中leg是 Legend 对象,ax是父 Axes 对象。您对我如何实现这一目标有什么想法吗?

python matplotlib legend

0
推荐指数
1
解决办法
5116
查看次数

使用 ggplot 的箱线图和图例标签的顺序

我想创建一个箱线图,ggplot2并且我想按照数据框的顺序组织图,例如

我知道 R 按字母顺序组织箱线图。我怎么能够:

  1. 按味道 - 颜色 - 容量的顺序组织 X 轴
  2. 交换盒子,即先是绿色,然后是橙色,而不是橙色和绿色
  3. 也切换图例顺序,首先是 NaCl,然后是 O_{2}
library(ggplot2)
library(readxl)

Chemical <- rep(c("NaCl", "Al2"), times = 3, each = 4)
Quality <- rep(c("Taste", "Color of package", "Capacity"), times = 1, each = 8)
Accepted <- seq(0, 100, by = 100/23)

DF <- data.frame(Chemical, Quality, Accepted)

ggplot(DF, aes(x = Quality, y = Accepted, fill = Chemical)) +
  geom_boxplot() +
  scale_fill_manual(values = c("orange", "green"),
                     labels = expression("Al"[2], "NaCl")) +
  xlab("") +
  theme(legend.position = …
Run Code Online (Sandbox Code Playgroud)

r legend ggplot2 boxplot

0
推荐指数
1
解决办法
3036
查看次数

图例句柄中的圆圈在 matplotlib 中不起作用

我在 Ubuntu 18.04 上,我的 matplotlib 版本是2.1.1. 我正在尝试绘制一个圆形补丁作为图形图例句柄。此示例提供了一种使用自定义句柄的方法,如下所示:

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt

red_patch = mpatches.Patch(color='red', label='The red data')
plt.legend(handles=[red_patch])

plt.show()
Run Code Online (Sandbox Code Playgroud)

但我想要一个圆形手柄而不是矩形补丁。所以我试过:

导入 matplotlib.pyplot 作为 plt 导入 matplotlib.patches 作为 mpatches

fig, ax = plt.subplots(1, 1)
circle = mpatches.Circle(xy = (0.5, 0.5), radius = 100,color = "green")

ax.plot([1, 2, 3], [1, 2, 3])

fig.legend(handles = [circle], labels = ["some funny label"])
plt.show()
Run Code Online (Sandbox Code Playgroud)

然而,我仍然得到一个矩形补丁,在我看来,在错误的位置。我到底错过了什么?

在此处输入图片说明

编辑:我特别问我的代码有什么问题。有解决方法很有帮助,但我看不出上面的代码有什么问题。

python matplotlib legend legend-properties

0
推荐指数
1
解决办法
2319
查看次数

R ggplot - 具有相同颜色变量的多行仅显示第一行的图例

我的数据(dt1)如下:

dt1 <- structure(list(date = structure(c(NA, 17179, 17180, 17181, 17182, 
17183, 17178, 17179, 17180, 17181, 17182, 17183), class = "Date"), 
    f = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L), y1 = c(68L, 
    43L, 99L, 53L, 12L, 20L, 29L, 49L, 68L, 15L, 71L, 88L), y2 = c(15L, 
    15L, 66L, 53L, 63L, 37L, 91L, 17L, 87L, 87L, 43L, 77L)), row.names = c(NA, 
-12L), class = "data.frame")

       date f y1 y2
1  12-01-17 0 68 15
2  13-01-17 …
Run Code Online (Sandbox Code Playgroud)

r legend ggplot2

0
推荐指数
1
解决办法
204
查看次数

如何向 ggplot 图例添加不同类型的元素?

在底部发布的代码中,我尝试向现有图例添加一个带有标签“置信区间”的灰色条。有人可以帮我将其添加到图例中吗?如果这可以添加到现有的传说中,那就太好了;如果只为这个项目添加一个新的图例更清晰,那也可以。作为旁注,对于渲染的简单绘图,我的 ggplot 代码看起来相当长且混乱,也许这就是 ggplot 代码总是最终的结果。我想知道 ggplot 与基本 R 图相比是否值得。

这张图片显示了我正在尝试做的事情: 在此输入图像描述

代码:

library(dplyr)
library(ggplot2)
library(MASS)
library(survival)

lung1 <- lung %>% 
  mutate(time1 = ifelse(time >= 500, 500, time)) %>% 
  mutate(status1 = ifelse(status == 2 & time >= 500, 1, status))

weibCurve <- function(time, survregCoefs) {exp(-(time/exp(survregCoefs[1]))^exp(-survregCoefs[2]))}

fit1 <- survreg(Surv(time1, status1) ~ 1, data = lung1)

lung1.survfit <- survfit(Surv(time1, status1) ~ 1, data = lung1)
lung1.df <- data.frame(time = lung1.survfit$time, 
                       survival = lung1.survfit$surv, 
                       upper_95 = lung1.survfit$upper, 
                       lower_95 = lung1.survfit$lower)

lung1.df %>%
  ggplot(aes(x = time, …
Run Code Online (Sandbox Code Playgroud)

r legend ggplot2

0
推荐指数
1
解决办法
98
查看次数

当每行使用不同的x和y值时,向ggplot2图添加图例

我的问题非常类似于添加图例到ggplot2线图,除了我有多种颜色的情况 - 每个条件1,但它们没有绘制到一个常见的x轴值.相反,我有2个x轴值,使用的值取决于条件.

示例数据:

# dataframe name = df

e0 e200 o0 o50 o200
0.5715707 0.5755010 0.9151736 0.8858229 1.2488826
0.5570928 0.5610231 0.8724417 0.8851889 1.2135041
0.5430821 0.5470124 0.8692482 0.8793603 1.2051914
0.5295093 0.5334396 0.8251555 0.8636917 1.0951763
0.5163479 0.5202782 0.8149114 0.8519220 1.0787246
0.5035736 0.5075039 0.7875460 0.7521003 1.0655470
0.4911643 0.4950946 0.7724218 0.7394516 1.0616154
0.4790998 0.4830301 0.7306038 0.6997307 1.0214771
0.4673614 0.4712917 0.6373668 0.6333903 0.9179331
0.4559320 0.4598622 0.5898641 0.6314342 0.8713423
0.4447956 0.4487259 0.5870693 0.6266098 0.8208793
Run Code Online (Sandbox Code Playgroud)

R脚本:

ggplot(df) +
geom_line(aes(x = e0,y = o0), size …
Run Code Online (Sandbox Code Playgroud)

r linegraph legend ggplot2

-1
推荐指数
1
解决办法
318
查看次数

ggplot 条形图图例中的项目顺序出现问题

我的数据看起来像

    language    tone        count   tone_percent    label_pos   pos
1   c           positive    3460    36.16977        18.08488    7
2   c           neutral     2046    21.38825        46.86389    7
3   c           negative    4060    42.44198        78.77901    7
4   c#          positive    3732    41.26949        20.63475    3
5   c#          neutral     1832    20.25876        51.39887    3
6   c#          negative    3479    38.47175        80.76413    3
7   c++         positive    3136    33.13960        16.56980    8
8   c++         neutral     2008    21.21949        43.74934    8
9   c++         negative    4319    45.64092        77.17954    8
Run Code Online (Sandbox Code Playgroud)

我一直在尝试使用 ggplot2 条形图将它们可视化:

p <-ggplot() + theme_bw() + geom_bar(aes(y=tone_percent, x=reorder(language, …
Run Code Online (Sandbox Code Playgroud)

r legend bar-chart ggplot2

-2
推荐指数
1
解决办法
2565
查看次数

在 JavaScript 中创建图例

如何使用 JavaScript 根据数组列表创建如下所示的颜色框?

在此处输入图片说明

html javascript legend

-3
推荐指数
1
解决办法
3761
查看次数