从数据框中散点图多列 python
Scatter plot multiple columns from dataframe python
- 我有一个数据框(97 列 x 30 行)。在这个数据框中
只有 1 和 0.
- 我想把它画成散点图,其中 x 轴是
列的名称和 y 轴中索引的名称。
[我的数据框是这样的][1]
- 我要的输出和照片差不多,但是红点必须是
只有当行和列之间的交集的值
具有 1 值。
- 如果有一个 0 值,则交集处不绘制任何内容。[][2][the
输出我想要的散点图][3]
- https://i.stack.imgur.com/hFnQX.png
- https://i.stack.imgur.com/Rsguk.jpg
- https://i.stack.imgur.com/keGC6.png
一个简单的方法是使用两个嵌套循环在每个数据框单元格上有条件地绘制点:
import pandas as pd
import matplotlib.pyplot as plt
example = pd.DataFrame({'column 1': [0, 1, 0, 1],
'column 2': [1, 0, 1, 0],
'column 3': [1, 1, 0, 0]})
for x, col in enumerate(example.columns):
for y, ind in enumerate(example.index):
if example.loc[ind, col]:
plt.plot(x, y, 'o', color='red')
plt.xticks(range(len(example.columns)), labels=example.columns)
plt.yticks(range(len(example)), labels=example.index)
plt.show()
- 我有一个数据框(97 列 x 30 行)。在这个数据框中 只有 1 和 0.
- 我想把它画成散点图,其中 x 轴是 列的名称和 y 轴中索引的名称。
[我的数据框是这样的][1]
- 我要的输出和照片差不多,但是红点必须是 只有当行和列之间的交集的值 具有 1 值。
- 如果有一个 0 值,则交集处不绘制任何内容。[][2][the 输出我想要的散点图][3]
- https://i.stack.imgur.com/hFnQX.png
- https://i.stack.imgur.com/Rsguk.jpg
- https://i.stack.imgur.com/keGC6.png
一个简单的方法是使用两个嵌套循环在每个数据框单元格上有条件地绘制点:
import pandas as pd
import matplotlib.pyplot as plt
example = pd.DataFrame({'column 1': [0, 1, 0, 1],
'column 2': [1, 0, 1, 0],
'column 3': [1, 1, 0, 0]})
for x, col in enumerate(example.columns):
for y, ind in enumerate(example.index):
if example.loc[ind, col]:
plt.plot(x, y, 'o', color='red')
plt.xticks(range(len(example.columns)), labels=example.columns)
plt.yticks(range(len(example)), labels=example.index)
plt.show()