Python 用x(而不是1)绘制包含所有1和0的数据框,0为空
我有一个熊猫数据框,看起来像:Python 用x(而不是1)绘制包含所有1和0的数据框,0为空,python,pandas,matplotlib,Python,Pandas,Matplotlib,我有一个熊猫数据框,看起来像: 0 1 2 3 4 0 1 1 1 0 1 1 1 0 1 1 1 2 1 0 0 1 0 3 1 1 1 0 0 4 0 1 0 0 0 x x x x x x x x x x x x x x 我想创建一个数据图(可能使用matplotlib),如下所示: 0 1 2 3 4 0 1 1 1 0
0 1 2 3 4
0 1 1 1 0 1
1 1 0 1 1 1
2 1 0 0 1 0
3 1 1 1 0 0
4 0 1 0 0 0
x x x x
x x x x
x x
x x x
x
我想创建一个数据图(可能使用matplotlib),如下所示:
0 1 2 3 4
0 1 1 1 0 1
1 1 0 1 1 1
2 1 0 0 1 0
3 1 1 1 0 0
4 0 1 0 0 0
x x x x
x x x x
x x
x x x
x
有人知道这样做的方法吗?绘图不需要由matplotlib生成,这可能会让您走上正确的轨道
import matplotlib.pyplot as plt
points = []
for index, row in df.iterrows():
for i,x in enumerate(row):
if x==1:
points.append([index,i])
df_plt = pd.DataFrame(points)
plt.scatter(df_plt[0],df_plt[1])
plt.show()
这应该有帮助plt.imshow(df.values)
可以。