I'm fairly new to matplotlib, and I'm having trouble putting a colorbar to my figure.
My current code is here:
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.cm as cm
import glob
from natsort import natsorted
import re
plt.xlabel('Displacement (A)')
plt.ylabel('Gs(r,t)')
plt.title('Van Hove Self Correlation')
color_num = 0
colors = cm.get_cmap('jet', len(sorted(glob.glob('van_hove_t=*.dat'))))
for filename in natsorted(glob.glob('van_hove_t=*.dat')):
print(filename)
df = pd.read_csv(filename, sep='\t', skiprows=1, )
#print(df)
color_num+=(1/len(sorted(glob.glob('van_hove_t=*.dat'))))
plt.plot( 'A^-1', 'count', data=df, color=colors(color_num), linewidth=1)
plt.xlim(0, 150)
plt.show()
plt.close()
It takes the output of several files and plots them in order, changing the color with each successive plot. Here is an example of the output:
I can't for the life of me figure out how to add a colorbar to this plot. I have tried plt.colorbar()
but I don't know which object to point it to in order for it to create the colorbar, colors
and plt
didn't work.