小编vfe*_*raz的帖子

Seaborn 虚线并非图例中的虚线

我有以下图表:

sns.set_context("paper", font_scale = 1)

ax = sns.lineplot(x="generation", y="fitness", hue="Quadrant", data=dfnash)
ax.lines[4].set_linestyle(":")
ax.set_xlabel("Generation")
ax.set_ylabel("Fitness Scores (Aggregated Expected Utility)")
#ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.tight_layout()
plt.savefig('nash7.png', dpi = 600, transparent=True)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我希望其中一条线是虚线,即“整个空间”系列,但图例仍然显示一条规则线。这是正常行为吗?我是否需要手动更改图例中的某些内容以遵循图表中的样式?

matplotlib python-3.x seaborn

4
推荐指数
1
解决办法
5320
查看次数

将多级列表列表展平为单级

我需要展平多级列表的所有级别:

import itertools

pool = [[[[[0,2],[1,3]],[[3,2],[1,0]]],"PdPd","PrisonerDilemma"], 
[[[[0,3],[1,2]],[[2,3],[1,0]]],"ShSh","StagHunt"],
[[[[1,2],[3,0]],[[3,2],[1,0]]],"ChCh","Chicken"],
[[[[2,1],[0,3]],[[3,1],[0,2]]],"BaBa","Battle"]]

def flat3 (pool):
    for game in pool:
        l = list(itertools.chain.from_iterable(game[0]))
        print(l)
Run Code Online (Sandbox Code Playgroud)

结果:

flat3 (pool)
[[0, 2], [1, 3], [3, 2], [1, 0]]
[[0, 3], [1, 2], [2, 3], [1, 0]]
[[1, 2], [3, 0], [3, 2], [1, 0]]
[[2, 1], [0, 3], [3, 1], [0, 2]]
Run Code Online (Sandbox Code Playgroud)

因此,目标是隔离并返回每个项目中的第一级,仅包含数字,例如:

[[[[[0,2],[1,3]],[[3,2],[1,0]]],"PdPd","PrisonerDilemma"]
Run Code Online (Sandbox Code Playgroud)

然后我需要将所有内容都展平到同一级别,例如:

[0,2,1,3,3,2,1,0]
Run Code Online (Sandbox Code Playgroud)

我知道有很多关于这个主题的材料,我已经找到并尝试了很多方法来做到这一点,但是似乎没有一个可以在一个以上的层次上工作,我想知道是否有一种有效的方法来做到这一点,而不重复多次执行相同的命令。有人可以帮助我吗?

python list concatenation flatten python-3.x

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

来自 Pandas 数据透视表的堆积面积图 (matplotlib)

我有以下格式的数据

import pandas as pd
import matplotlib.pyplot as plt

    Metric  Country  Year    Value
0       2G  Austria  2018  1049522
1       2G  Austria  2019   740746
2       2G  Austria  2020   508452
3       2G  Austria  2021   343667
4       2G  Austria  2022   234456
65      3G  Austria  2018  2133823
66      3G  Austria  2019  1406927
67      3G  Austria  2020  1164042
68      3G  Austria  2021  1043169
69      3G  Austria  2022   920025
130     4G  Austria  2018  7482733
131     4G  Austria  2019  8551865
132     4G  Austria  2020  8982975
133     4G …
Run Code Online (Sandbox Code Playgroud)

matplotlib python-3.x pandas

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