Chathura Madhusanka
Chathura Madhusanka

Reputation: 108

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

enter image description here 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!

Upvotes: 2

Views: 8064

Answers (2)

Martin Evans
Martin Evans

Reputation: 46759

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 heatmap

Upvotes: 1

Pineapples
Pineapples

Reputation: 66

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

Upvotes: 1

Related Questions