简体   繁体   English

Pandas dataframe 散点图 plot 以 2 级多索引为轴

[英]Pandas dataframe scatter plot with 2-level Multiindex as axes

I have a dataframe df with a 2-level Multiindex.我有一个带有 2 级多索引的 dataframe df I want a scatter plot with level 0 on the x-axis and level 1 on the y axis and scattered dots for all combinations which satisfy a condition, say have a nonzero value in a specific column 'col' .我想要一个散点图 plot,x 轴为 0 级,y 轴为 1 级,所有满足条件的组合的散点,比如在特定列'col'中具有非零值。

import matplotlib.pyplot as plt
from itertools import product
import numpy as np

lengths = [3, 2]
df_index = pd.MultiIndex.from_product([list(product([-1,1], repeat=li)) for li in lengths], names=['level1', 'level2'])

df_cols = ['cols']
df = pd.DataFrame([[0.] * len(df_cols)] * len(df_index), index=df_index, columns=df_cols)
df['cols'] = np.random.randint(0, 2, size = len(df))
df

yields a dataframe of the following form产生以下形式的 dataframe

                       cols
level1       level2        
(-1, -1, -1) (-1, -1)     0
             (-1, 1)      0
             (1, -1)      0
             (1, 1)       0
(-1, -1, 1)  (-1, -1)     1
             (-1, 1)      0
             (1, -1)      1
             (1, 1)       1
(-1, 1, -1)  (-1, -1)     0
             (-1, 1)      0
             (1, -1)      0
             (1, 1)       0
(-1, 1, 1)   (-1, -1)     0
             (-1, 1)      0
             (1, -1)      1
             (1, 1)       0
(1, -1, -1)  (-1, -1)     0
             (-1, 1)      0
             (1, -1)      1
             (1, 1)       1
(1, -1, 1)   (-1, -1)     0
             (-1, 1)      1
             (1, -1)      1
             (1, 1)       0

... ...

Now, I want a scatter plot with the level1 index on the x-axis and the level2 index on the y-axis such that for every (x,y) with cols(x,y).= 0 there is a dot.现在,我想要一个散点图 plot,x 轴上的 level1 索引和 y 轴上的 level2 索引,这样对于 cols(x,y).= 0 的每个 (x,y) 都有一个点。

Let's first create an example dataframe with 2-level Multiindex:让我们首先创建一个具有 2 级多索引的示例 dataframe:

import pandas as pd
import numpy as np
iterables = [[1, 2, 3, 4], [0,1, 2, 3, 4,5]]
my_multiindex=pd.MultiIndex.from_product(iterables, names=['first', 'second'])
series1 = pd.Series(np.random.randn(24), index=my_multiindex)
series2 = pd.Series(np.random.randn(24), index=my_multiindex)
df=pd.DataFrame({'col1':series1,'col2':series2})

Now, let's get the index values that satisfy a given condition:现在,让我们获取满足给定条件的索引值:

index_values=df[df.col1<0].index.values

We then separate x and y coordinates:然后我们分开xy坐标:

xs=[a[0] for a in index_values]
ys=[a[1] for a in index_values]

We then plot:然后我们 plot:

from matplotlib import pyplot as plt
plt.scatter(xs,ys)

If you want the size of the scatter dots to reflect the actual values, you can use:如果您希望散点的大小反映实际值,您可以使用:

column_values=abs(df[df.col1<0].col1.values)
plt.scatter(xs,ys,s=column_values*10)

Edit to reflect the edited question :编辑以反映已编辑的问题

You would just need to convert your xs and ys to strings.您只需要将xsys转换为字符串。 I am also using a large figure so that axis tick labels don't overlap:我还使用了一个大图,以便轴刻度标签不重叠:

plt.figure(figsize=(10,10))
plt.scatter([str(a) for a in xs],[str(a) for a in ys])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM