一、需求
根據(jù)可視化的需要,下圖的數(shù)據(jù)需要使用棋盤(pán)圖的樣式來(lái)展示,
原始數(shù)據(jù):
最終效果圖:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-578535.html
二、處理方式
1、先將DataFrame數(shù)據(jù)轉(zhuǎn)換為Numpy數(shù)組;
2、先使用np.transpose函數(shù),找到0和1值的索引;
3、然后創(chuàng)建散點(diǎn)圖;
4、完成散點(diǎn)圖后,由于需要展示的坐標(biāo)值是文本,所以在繪制坐標(biāo)刻度時(shí),plt.xticks和plt.yticks要使用第二個(gè)參數(shù)傳遞,使其顯示的是文本。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-578535.html
三、代碼實(shí)現(xiàn)
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# 提供的 DataFrame 數(shù)據(jù)
data = pd.read_excel('demo.xlsx',index_col=0)
# 將 DataFrame 轉(zhuǎn)換為 NumPy 數(shù)組
data_array = data.values
# 找到值為 0 和 1 的索引
zero_indices = np.transpose(np.nonzero(data_array == 0))
one_indices = np.transpose(np.nonzero(data_array == 1))
# 創(chuàng)建散點(diǎn)圖
plt.scatter(one_indices[:, 1] + 1, one_indices[:, 0] + 1, c='black', marker='o')
# 修改刻度值顯示值
plt.xticks(np.arange(1, data.shape[1]+1),data.columns)
plt.yticks(np.arange(1, data.shape[0]+1),data.index)
# 繪制網(wǎng)格線
plt.grid(True,which='both')
# 設(shè)置圖表標(biāo)題和軸標(biāo)簽
plt.title('Checkerboard Diagram')
plt.xlabel('X')
plt.ylabel('Y')
# 顯示圖例
plt.legend()
# 顯示圖表
plt.show()
到了這里,關(guān)于python根據(jù)excel數(shù)據(jù),基于散點(diǎn)圖繪制棋盤(pán)圖的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!