Skip to content

Instantly share code, notes, and snippets.

@ShayanRiyaz
Created April 24, 2020 10:33
Show Gist options
  • Save ShayanRiyaz/469097deacfe2f64cc839935fc1c2523 to your computer and use it in GitHub Desktop.
Save ShayanRiyaz/469097deacfe2f64cc839935fc1c2523 to your computer and use it in GitHub Desktop.
def generate_plot(clus,i):
plt.style.use('default')
tags=['Restaurant','Coffee','Food','Pizza','Sandwich']
colors = []
for value in clus.index:
if any(t in value for t in tags):
colors.append('#0000FF')
else:
colors.append('#FF0000')
ax=clus.plot(kind='barh', figsize=(16,8), color=colors, alpha=0.7)
plt.title('(in % of all venues)\n')
ax.title.set_fontsize(14)
plt.suptitle('Ten Most Prevalent Venues of Cluster {}'.format(i), fontsize=16)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
plt.xticks([])
ax.tick_params(axis ='both', which ='both', length = 0)
labels = [(item.get_text()+' ') for item in ax.get_yticklabels()]
ax.set_yticklabels(labels)
for label in (ax.get_yticklabels()):
label.set_fontsize(12)
for index, value in enumerate(clus):
label = "%.1f " % round(value*100,1) + "%"
# place text at the end of bar (adding 0.001 to x, and 0.1 from y to make it appear just after the bar)
plt.annotate(label, xy=(value + 0.001, index - 0.1), color='black',fontsize=12)
legend_elements = [Patch(facecolor='#0000FF', edgecolor='#0000FF',
label='Food Venues',alpha=0.7),
Patch(facecolor='#FF0000', edgecolor='#FF0000',
label='Others',alpha=0.7)]
ax.legend(handles=legend_elements, loc='best',fontsize=12)
plt.show()
cluster1=pd.DataFrame(la_results.iloc[1,0:-1]).transpose()
cluster1.sort_values(by='Cluster 1',axis=1,ascending=False,inplace=True)
display(cluster1)
clus1=cluster1.iloc[0,9::-1]
generate_plot(clus1,1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment