0

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

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.

  • well, per se it is not possible. I would create a vertical bar chart as inset and put it anywhere. Then it looks like a colorbar. Or create an image instead and use an imshow inset to show this, then it is nicely interpolated. – anki Jan 24 '19 at 19:30
  • @anki That's wrong. Sure it's possible. See [this answer](https://stackoverflow.com/a/11558629/4124317). In general, consider all answers of the duplicate, except the accepted one. – ImportanceOfBeingErnest Jan 24 '19 at 19:32
  • @ImportanceOfBeingErnest, thanks for pointing that out to me. Again what learned! – anki Jan 24 '19 at 19:36

0 Answers0