如果我有这种类型的字典:
a_dictionary = {"dog": [["white", 3, 5], ["black", 6,7], ["Brown", 23,1]],
"cat": [["gray", 5, 6], ["brown", 4,9]],
"bird": [["blue", 3,5], ["green", 1,2], ["yellow", 4,9]],
"mouse": [["gray", 3,4]]
}
Run Code Online (Sandbox Code Playgroud)
我想从第一行 3 与 6 和 23 求和,在下一行 5 与 4 等求和,这样打印时我会得到:
dog [32, 13]
cat [9, 15]
bird [8, 16]
mouse [3,4]
Run Code Online (Sandbox Code Playgroud)
我尝试了a_dictionary 范围的for 循环来按索引求和,但随后我无法通过以下键访问值:a_dictionary[key]
但是,如果我像这样循环遍历 a_dictionary for key, value in a dictionary.items():,我无法通过索引访问它来总结所需的值。
我很想看看如何实现这一点。谢谢。
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# generate data
rng = np.random.default_rng(12)
x = np.linspace(0, np.pi, 50)
y1, y2 = np.sin(x[:25]), np.cos(x[25:])
cat = rng.choice(["a", "b", "c"], 50)
data = pd.DataFrame({"x": x , "y" : y1.tolist() + y2.tolist(), "cat": cat})
# generate figure
fig, ax = plt.subplots(figsize=(8, 5), ncols=2)
g0 = sns.lineplot(x="x", y="y", data=data, hue='cat', ls="--", ax=ax[0])
g1 = sns.lineplot(x="x", y="y", data=data, hue='cat', ls="--", ax=ax[1])
g1.get_lines()[0].set_color('black')
print(ax[0].get_lines())
for line in …Run Code Online (Sandbox Code Playgroud) 我想减少图例列之间的空间(附图中显示了一个示例)。所以,我想做的是,
[前]
(对称)A ------ (对称)B ------ (对称)C ------ (对称)D
[后]
(对称)A -- (对称)B -- (对称)C -- (对称)D
有办法做到吗?(例如,plt.legend(ncol=4, [a hidden parameter??]))
谢谢!
当前输出的图像:
与在 Altair 图表中设置颜色类似,我使用分层图表,并且喜欢设置显式颜色。不知何故,当我使用答案中的解决方案时,我的颜色总是变成一些任意颜色。我想知道,如果有人知道,为什么会发生这种情况以及如何强制 Altair 使用颜色数组中给出的颜色。
这是一个测试示例:
import altair as alt
from vega_datasets import data
source = data.movies()
colors = ['#170c3b', '#fa8d0b']
plot = alt.Chart(source).mark_line().encode(
x=alt.X("IMDB_Rating", bin=True),
y=alt.Y(
alt.repeat("layer"), aggregate="mean", title="Mean of US and Worldwide Gross"
),
color=alt.datum(alt.repeat("layer")),
).repeat(
layer=["US_Gross", "Worldwide_Gross"]
).configure_range(
category=alt.RangeScheme(scheme=colors)
)
plot.show()
Run Code Online (Sandbox Code Playgroud)
这是数组中的颜色: