Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将DataFrame的列设置为FacetGrid图形的行_Python_Pandas_Seaborn_Facet Grid - Fatal编程技术网

Python 将DataFrame的列设置为FacetGrid图形的行

Python 将DataFrame的列设置为FacetGrid图形的行,python,pandas,seaborn,facet-grid,Python,Pandas,Seaborn,Facet Grid,我有一个国家小组数据集,其中有每年国家观察的若干指标。为简单起见,我在此仅报告两个指标:温室气体和空气排放 rs = np.random.RandomState(4) pos = rs.randint(-1, 2, (4, 5)).cumsum(axis=1) pos -= pos[:, 0, np.newaxis] pos2 = rs.randint(-4, 3, (4, 5)).cumsum(axis=1) pos2 -= pos[:, 0, np.newaxis] year = np.ti

我有一个国家小组数据集,其中有每年国家观察的若干指标。为简单起见,我在此仅报告两个指标:温室气体和空气排放

rs = np.random.RandomState(4)
pos = rs.randint(-1, 2, (4, 5)).cumsum(axis=1)
pos -= pos[:, 0, np.newaxis]
pos2 = rs.randint(-4, 3, (4, 5)).cumsum(axis=1)
pos2 -= pos[:, 0, np.newaxis]
year = np.tile(range(5), 4)
walk = np.repeat(range(4), 5)

df = pd.DataFrame(np.c_[pos.flat, pos2.flat, year, walk],
                  columns=["Air emissions", 'GHG', "year", "Country ID"])
我想开发一个可视化系统,显示每个国家每年每个指标的趋势。每个指标显示在一行中,而国家是我的列。到目前为止,这是我为一个指标——空气排放——所做的工作,但我还想展示温室气体的趋势(以及这里未报告的其他指标),并将它们添加为空气排放:如何

sns.set(style="ticks")

    # Initialize a grid of plots with an Axes for each walk
grid = sns.FacetGrid(df, col="Country ID", hue="year", palette="tab20c",
                         col_wrap=4, height=3)

    # Draw a line plot to show the trajectory of each random walk
grid.map(plt.plot, "year", "Air emissions",  marker="o")

    # Adjust the arrangement of the plots
grid.fig.tight_layout(w_pad=1)
我怎么做?循环?但这不会覆盖图表吗


谢谢

您需要将FacetGrid行中的变量编码为一列,其中包含每个变量的值。可能不是最好的解释,但看起来是这样的:

    year  Country ID       variable  value
0      0           0  Air emissions      0
1      0           0            GHG      0
2      0           1  Air emissions      0
3      0           1            GHG     -3
4      0           2  Air emissions      0
5      0           2            GHG     -2
...
然后,您可以将FacetGrid参数
设置为
变量
(您还必须删除
列包装
):


您可以使用透视表重新格式化数据帧:

df = df.pivot_table(index=['year', 'Country ID'], values=['Air emissions', 'GHG']).stack().reset_index()
df.columns = ['year', 'Country ID', 'variable', 'value']

您需要将想要在FacetGrid行中的变量编码为一列,其中包含每个变量的值的单独列。可能不是最好的解释,但看起来是这样的:

    year  Country ID       variable  value
0      0           0  Air emissions      0
1      0           0            GHG      0
2      0           1  Air emissions      0
3      0           1            GHG     -3
4      0           2  Air emissions      0
5      0           2            GHG     -2
...
然后,您可以将FacetGrid参数
设置为
变量
(您还必须删除
列包装
):


您可以使用透视表重新格式化数据帧:

df = df.pivot_table(index=['year', 'Country ID'], values=['Air emissions', 'GHG']).stack().reset_index()
df.columns = ['year', 'Country ID', 'variable', 'value']

我不确定你的温室气体数据在哪里。是“其他”列吗?是的,对不起,更正了!我不确定你的温室气体数据在哪里。是“其他”列吗?是的,对不起,更正了!谢谢,您知道如何将违规名称放在每行的y轴上,而不是将其作为标题吗?违规名称和国家/地区重叠,无法清晰阅读。您可以使用
网格访问每个轴。轴[i,j]
。这样,您可以使用常用的
set_ylabel
set_title
来更改这些值。您可能希望使用
row\u order
为FacetGrid显式设置行顺序,这样您就可以确切地知道每行代表哪个变量。谢谢,您知道如何将违规名称放在每行的y轴上,而不是将其作为标题吗?违规名称和国家/地区重叠,无法清晰阅读。您可以使用
网格访问每个轴。轴[i,j]
。这样,您可以使用常用的
set_ylabel
set_title
来更改这些值。您可能希望使用
行顺序
为FacetGrid显式设置行顺序,以便准确地知道每行代表哪个变量。