'matplotlib's 'tab20' color repeats color after 10
I am plotting the distribution of a variable say a RMSF from multiple files into one histogram, such that each file belongs to a cluster. I have 25 clusters at the moment. The goal is to plot a single histograms for all clusters, coloring the bars by their clusters. I have a working code but even after using a color palette such as tab20 of matplotlib, I see that the colors repeat themselves after 10. My goal is to assign a distinct color to each cluster. At the moment I have 25, but I might have even more later. Following is my code:
import glob
from matplotlib import figure
import matplotlib.pypot as plt
import matplotlib.cm as mplcm
import matplotlib.colors as colors
import os
import pandas as pd
import matplotlib.pyplot as plt
filelist = glob.glob('alkenes_testrg_cluster*.csv')
filename = [((i.split('alkenes_testrg_')[1]).split('.')[0]).split('_')[0] for i in filelist]
cm = plt.get_cmap('tab20',25)
NUM_COLORS=25
sns.set(color_codes=True)
sns.set(rc={"xtick.bottom" : True, "ytick.left" : True})
sns.set_style('darkgrid')
#cNorm = colors.Normalize(vmin=0, vmax=NUM_COLORS-1)
#scalarMap = mplcm.ScalarMappable(norm=cNorm, cmap=cm)
fig,ax = plt.subplots(figsize=(12,8))
ax.set_prop_cycle('color', [cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
for i,filen in enumerate(filelist):
### Read .csv file and append to list
df = pd.read_csv(filen)
bins=np.arange(df.RMSF_TOTAL.min(), df.RMSF_TOTAL.max(),0.005)
if len(df.RMSF_TOTAL) > 5:
plt.hist(df.RMSF_TOTAL,bins=bins,label=filename[i],alpha=0.9,rwidth=1)
#print('cluster'+str(i+1),filen)
### Generate the plot
sns.set(color_codes=True)
plt.xlabel("RMSF", fontsize=20)
plt.ylabel("Count", fontsize=20)
plt.yticks(fontsize=20)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.title('Raregas RMSF Distribution of'+'\n Parameter Based Clusters',fontsize=25)
ax.legend(fontsize=20, bbox_to_anchor=(1.0, 1.0),ncol=2)
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
