我正在尝试为使用 ggplot2 制作的复合图表生成一个很好的图例,其中有一个躲避的条形图和一条水平线,用于显示土壤中钡的参考值。问题是 hline 图例的特征也“污染”了条形图。我已经看到类似的主题试图调整提出的其他解决方案,但没有任何显着结果。
这是我的例子:
数据框:
Position Year Value
Position 1 1999 12
Position 2 1999 14
Position 3 1999 15
Position 1 2000 13
Position 2 2000 11
Position 3 2000 21
Position 1 2001 12
Position 2 2001 13
Position 3 2001 16
Run Code Online (Sandbox Code Playgroud)
编码:
ggplot(input, aes(fill=Year , x=Position, y=Value)) +
geom_bar(stat="identity",
position="dodge", size=0.1, width=0.5, show.legend=TRUE) +
labs(title="Barium concentration in the soil") +
xlab("Samples") +
ylab("Concentration in ng/kg") +
scale_y_continuous(limits = c(0, 30)) +
#color of barplot
scale_fill_manual(values=c("grey40", …
Run Code Online (Sandbox Code Playgroud) 我在 VS2017 中调整 C# 图表图例的布局时遇到困难。当我进入时Legends
,Properties
我可以更改几个属性,但我找不到如何更改图例边框与其项目之间的边距(因此,实际上是填充)。我无法更改的另一个属性:图例项之间的间距。我发现了一些关于“样式文件”的讨论,但我想避免这种情况。我还搜索了可以给我更多选择的不同参考资料,但我什么也没找到。
那么,有没有办法更改图例的属性,而不是 中显示的属性Properties
?我希望找到类似的东西:“myLegend.padding = 10pt”。
我正在尝试向我的圆环图添加 D3 图例,我按照这里的示例进行操作:http : //bl.ocks.org/ZJONSSON/3918369。圆环图呈现良好。
但是,我遇到了以下错误:
在 d3.legend() 函数中,我收到此错误“Uncaught TypeError: node.getAttribute is not a function”和“Uncaught TypeError: this.querySelectorAll is not a function”。
我不知道为什么......有什么想法吗?
// Alerts.js
renderBody() {
const {list, currentProgram} = this.props
const width = 260;
const height = 260;
const radius = width/2; // 130
// arc & label generator
let arc = d3.arc()
.outerRadius(radius - 10)
.innerRadius(90)
.padAngle(0.02);
let labelArc = d3.arc()
.outerRadius(radius + 60)
.innerRadius(radius - 90);
// pie generator
let pie = d3.pie()
.sort(null)
.value( …
Run Code Online (Sandbox Code Playgroud) 有没有办法根据边缘颜色(而不是节点颜色)在 networkx 中创建图例?
这是我的图表:
plt.figure(figsize = (15, 10))
G = nx.from_pandas_dataframe(df, 'From', 'To', ['Order', 'Colors'])
edge_labels = nx.get_edge_attributes(G, 'Order')
nx.draw_networkx(G, with_labels = False, node_color = 'black', alpha = 0.5, node_size = 3, linewidths = 1, edge_color = df['Colors'], edge_cmap =
plt.cm.Set2)
plt.show()
Run Code Online (Sandbox Code Playgroud)
其中,['Order']
是边缘的描述符,['Color']
是映射到 中每个值的唯一整数['Order']
,它致力于基于 Set2 颜色图创建边缘颜色。
我可以使用以下内容获取边缘标签:
edge_labels = nx.get_edge_attributes(G, 'Order')
但是如何将其放入图例中?
如果有帮助,我很乐意分享数据和完整代码!
我有几个要添加单个颜色条的子图。每个子图由 7 个散点组成。我找到了关于如何添加颜色条的建议,但它们主要与每个散点的值有关,而不是与行本身有关。
代表性示例代码:
import numpy as np
from matplotlib import pyplot as plt
x = range(50)
scales = np.linspace(0, 2, 7)
locs = range(4)
cmap = plt.get_cmap("Spectral")
for s_plot in range(4):
plt.subplot(2, 2, s_plot+1)
color = iter(cmap(np.linspace(0, 1, len(scales))))
for scale in scales:
c = next(color)
y = np.random.normal(loc=locs[s_plot], scale=scale, size=50)
plt.scatter(x, y, c=c, s=5)
plt.title("Mean = {:d}".format(locs[s_plot]))
plt.subplots_adjust(hspace=0.4)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我想要的颜色条看起来像这样(假的,放在情节旁边):
所以颜色条没有描述我的散点的值,而是迭代的不同“行”(在这种情况下:不同的比例)。在有助于将点与比例匹配的示例中。
我尝试的是一个简单的
plt.colorbar()
Run Code Online (Sandbox Code Playgroud)
在完成每个子图后调用一次。但我也得到了
TypeError: You must first set_array for mappable
,因为它是我想为其创建颜色图的不同比例,我也尝试过
plt.colorbar(scales)
Run Code Online (Sandbox Code Playgroud)
返回:AttributeError: 'numpy.ndarray' object …
概述:
我使用下面的R 代码制作了一系列地图,并使用Cowplot 包中的 plot_grid()使用下面称为“QuercusRobur1”和“QuercusRobur2”的数据框将图排列到一个绘图窗口上。
情节看起来非常好;然而,每个地块的图例都没有对齐,看起来不整齐。
有谁知道如何使用 plot_grid 在新的绘图排列中垂直向下对齐这些图例?
如果有人可以提供帮助,我将不胜感激
代码
##Import Packages
library(ggplot2)
library(maps)
library(mapdata)
library(tidyverse)
##Get a map of the UK from maps:
UK <- map_data(map = "world", region = "UK")
head(UK)
dim(UK)
##Produce point data
MapUK<-ggplot(data = UK, aes(x = long, y = lat, group = group)) +
geom_polygon() +
coord_map()
##head
head(QuercusRobur1)
head(QuercusRobur2)
##Remove weird data points
QuercusRobur1<-QuercusRobur1%>%filter(Longitude<=3)
##Observation 1
p1 <- ggplot(QuercusRobur1,
aes(x = Longitude, …
Run Code Online (Sandbox Code Playgroud) 此前legend
标签都有效有一个align
与值属性top
,bottom
,left
和right
。但在 HTML 5 中不再支持此属性。W3schools建议改用 css。
但是如何使用 css 将图例定位到字段集的底部,使其看起来像这样:
编辑:只是为了澄清:我并不是说legend
HTML 5 不支持该标签,但该align
属性不再存在。我想将图例放在字段集的底部。
我在泰坦尼克号数据集上使用 seaborn 散点图和计数图。
这是我绘制散点图的代码。我还尝试编辑图例标签。
ax = seaborn.countplot(x='class', hue='who', data=titanic)
legend_labels, _= ax.get_legend_handles_labels()
pyplot.show();
Run Code Online (Sandbox Code Playgroud)
为了编辑图例标签,我这样做了。在这种情况下,不再有图例标题。如何将此标题从 'who' 重命名为 'who1' ?
ax = seaborn.countplot(x='class', hue='who', data=titanic)
legend_labels, _= ax.get_legend_handles_labels()
ax.legend(legend_labels, ['man1','woman1','child1'], bbox_to_anchor=(1,1))
pyplot.show();
Run Code Online (Sandbox Code Playgroud)
我使用相同的方法编辑散点图上的图例标签,结果在这里有所不同。它使用 'dead' 作为图例标题,并使用 'survied' 作为第一个图例标签。
ax = seaborn.scatterplot(x='age', y='fare', data=titanic, hue = 'survived')
legend_labels, _= ax.get_legend_handles_labels()
ax.legend(legend_labels, ['dead', 'survived'],bbox_to_anchor=(1.26,1))
pyplot.show();
Run Code Online (Sandbox Code Playgroud)
(1) 是否有删除和添加图例标题的参数?
(2) 我在两个不同的图上使用了相同的代码,图例的结果是不同的。为什么?
谢谢
''' 为简单起见,让我们使用 iris 数据集。我想添加一个将每个物种与其颜色代码匹配的图例(在本例中为蓝色、绿色、红色)。顺便说一句,我在以下链接中发现了类似的问题,但更复杂一些。 如何在 Seaborn 中的热图轴上表达类
在Seaborn clustermap row color with legend 中提出的解决方案会起作用,但是对于 df[['tissue type','label']] 在定义 legend_TN 时,我不确定如何类似地定义标签,例如 iris['species' ,'xxxx'] 预先感谢您帮助我。'''
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
iris = sns.load_dataset('iris')
species = iris.pop('species')
lut = dict(zip(species.unique(), "rbg"))
row_colors = species.map(lut)
g = sns.clustermap(iris, row_colors=row_colors)
plt.show()
Run Code Online (Sandbox Code Playgroud)