Simple 2D histogram - NorrisLab/DLC_Python_Scripts GitHub Wiki

How to generate a 2D histogram from a CSV file

Code in reference: link to code

Note that this is a simple 2D histogram and not the histogram generated from a deeplabcut file which can be found here

Basic imports:

import pandas as pd

import matplotlib.pyplot as plt


The path refers to the location of the file, this script uses a CSV file to generate the histogram. Note that there is also an r before the path name is pasted. This is important for proper file recognition.

df = pd.read_csv(path, names=['x', 'y']) will read the CSV assuming there are two columns, an X and a Y column. Change the names field to what you name your two columns. The csv file should look somewhat like this with two columns being x and y:

x y
357 244
359 275

plt.hist2d(df['x'], df['y'], bins=50) the actual line of code that plots the histogram. If you changed the column name earlier (i.e. changed x and y to something else, change it here too!)

If you need the histogram to be more or less detailed change the number following bins. For example:

bins = 10 bins = 50 bins = 100

plt.title("2D histogram from CSV file") the title can be changed plt.show() will display the plot