0

I am trying to do a histogram like the one below but I am struggling with the bins. This is my code:

plt.subplots(figsize=(2, 1), dpi=400)
width = 0.005

plt.xticks(((density_1.index.unique()) | set(density_2.index.unique())), rotation=90, fontsize=1.5)
plt.yticks(list(set(density_1.unique()) | set(density_2.unique())), fontsize=2)
plt.hist(density_1.index, density_1, width, color='Green', label=condition_1,alpha=0.5)
plt.hist(density_2.index, density_2, width, color='Red', label=condition_2,alpha=0.5,bins=my_beans1)
plt.legend(loc="upper right", fontsize=2)
plt.show()

Those are my pandas:

1st Data sample:

 Xticks  Yticks
 0.27    0.068182
 0.58    0.045455
 0.32    0.045455
 0.47    0.045455
 0.75    0.045455
 0.17    0.045455
 0.43    0.022727
 0.66    0.022727
 0.11    0.022727
 0.68    0.022727
 0.59    0.022727

2nd Data sample:

 Xticks  Yticks
0.94    0.058442
0.86    0.058442
0.74    0.045455
0.93    0.045455
0.99    0.045455
0.71    0.038961
0.63    0.019481
0.97    0.019481
0.87    0.019481
0.84    0.019481
0.75    0.019481
0.89    0.019481
0.80    0.012987

I did this picture by using plt.bar() but I need to do with plt.hist. That is for the full dataset, but I am providing a sample of my dataframe to make it shorter.

I saw some forums and webs to do hist and the use of bins but I always get errors.

I tried something like this:

my_bins1=density_2.unique()
my_bins2=10
Ignacio Such
  • 129
  • 1
  • 8

1 Answers1

1

Assuming that you want to use a histogram to count the frequency of y-ticks, something like this might be what you are looking for:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd

data1 = pd.read_csv(r"file1", sep='\t')
data2 = pd.read_csv(r"file2", sep='\t')

data1 = data1.set_index('x_ticks')
data2 = data2.set_index('x_ticks')

plt.figure()

bins=np.linspace(0, 0.1, num=100)

n, bins, rectangles = plt.hist(data1, bins, color = 'green', alpha=0.5, label='dataset 1')
plt.hist(data2, bins, color = 'red', alpha=0.5, label='dataset 2')

plt.legend(loc='upper right')

plt.title('frequency of y-ticks')
plt.show()

Output looks like this:

histogram

LynneKLR
  • 90
  • 1
  • 8