简体   繁体   中英

Plotting a heatmap using CSV file data in python

I have output nested dictionary variable called all_count_details_dictionary . Using that variable I saved data to the CSV file using the following command.

import pandas as pd
csv_path = '../results_v6/output_01.csv'
# creating pandas dataframe using concat mehtod to extract data from dictionary
df = pd.concat([pd.DataFrame(l) for l in all_count_details_dictionary],axis=1).T

# saving the dataframe to the csv file
df.to_csv(csv_path, index=True)

The output CSV file is just like as below

在此处输入图片说明 The CSV file can be download using this link

So I used the following code to plot a graph

import matplotlib.pyplot as plt

def extract_csv_gen_plot(csv_path):

    length = 1503 #len(dataframe_colums_list)
    data = np.genfromtxt(csv_path, delimiter=",", skip_header=True, usecols=range(3, (length+1)))
    print(data)

    # renaming data axes
    #fig, ax = plt.subplots()
    #fig.canvas.draw()

    #labels =[item.get_text() for item in ax.get_xticklabels()]
    #labels[1] = 'testing'
    #ax.set_xticklabels(labels)

    #ax.set_xticklabels(list)
    #ax.set_yticklabels(list)
    #plt.setp(ax.get_xticklabels(), rotation = 90)


    plt.imshow(data, cmap='hot',interpolation='nearest')
    plt.show()

I tried to get the column labels and case details labels into the graph axes, but it doesn't work out. Can anyone please tell me there is any other best method to plot this table into a heat map than this?

Thank you!

I recommend using Seaborn, they have a heatmap plotting function that works very well with Pandas DataFrames

import seaborn as sns

sns.heatmap(data)

https://seaborn.pydata.org/generated/seaborn.heatmap.html

I would suggest using Pandas, the labels are picked up automatically:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

def extract_csv_gen_plot(csv_path):

    data = pd.read_csv(csv_path, index_col=1)
    data = data.drop(data.columns[[0, 1]], axis=1)
    data.index.names = ['Name']
    g = sns.heatmap(data)
    g.set_yticklabels(g.get_yticklabels(), rotation=0)
    g.set_title('Heatmap')
    plt.tight_layout()
    plt.show()


extract_csv_gen_plot("output_01.csv")

seaborn 热图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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