matplotlib bar chart with highlights values only matplotlib bar chart with highlights values only pandas pandas

matplotlib bar chart with highlights values only


Run through the x-ticks and then disable few of them based on countries list.

import numpy as npimport pandas as pd#Overall Country listCountries=['United States','Mexico','Japan','China','Korea,Rep.','Ireland','France','Italy','Czech Republic', 'Austria', 'Slovak Republic', 'Slovenia', 'Germany', 'Portugal', 'Hungary', 'Colombia', 'New Zealand', 'Norway', 'Latvia']#Countries to highlightDesired=['United States','Mexico','Japan','China','Korea,Rep.','Ireland','France','Italy']np.random.seed(0)Value=np.random.rand(len(Countries))df = pd.DataFrame({'Countries': Countries,'Value': Value,})df.sort_values(['Value'],inplace=True)df.set_index('Countries',drop=True,inplace=True)ax_1 = df['Value'].plot(kind='bar', title ="graph", figsize=(10, 6), fontsize=12)ax_1.set_xlabel("Country Name", fontsize=12)for ticks in ax_1.xaxis.get_major_ticks():    if ticks.label1.get_text() not in Desired:        ticks.label1.set_visible(False)        ax_1.patches[df.index.get_indexer([ticks.label1.get_text()])[0]].set_facecolor('w')        ax_1.patches[df.index.get_indexer([ticks.label1.get_text()])[0]].set_edgecolor('black')    else:        ax_1.patches[df.index.get_indexer([ticks.label1.get_text()])[0]].set_facecolor('r')        

enter image description here