python - Matplotlib Fill Missing Tic Labels -


i have list of pandas dataframes (example) data frames:

df1 = pd.dataframe({'number':[-9,-8,0,1,2,3], 'a':[3,6,4,1,7,19], 'b':[2,4,4,0,7,1]}) df1.set_index('number',inplace=true)  df2 = pd.dataframe({'number':[0,5,6,7,8,9], 'a':[8,7,3,5,2,15], 'b':[1,7,1,1,1,3]}) df2.set_index('number',inplace=true)  df_list = [df1, df2] #in reality there more 2 in list 

and trying plot them using matplotlib:

nrow = 2 ncol = 2 fig, axs = plt.subplots(nrow,ncol)  in range(nrow*ncol):     #convert 1d 2d     row = / ncol     col = % ncol     if >= len(df_list):         axs[row,col].axis('off')     else:         df_list[i]['a'].plot(kind='bar',               ax=axs[row,col],               ylim=(0,20),               xlim=(-10,10),               figsize=(20,15),               color=('green'),               legend=false,               )         df_list[i]['b'].plot(kind='bar',               ax=axs[row,col],               ylim=(0,20),               xlim=(-10,10),               figsize=(20,15),               color=('yellow'),               legend=false,               )  

the resulting plots this: enter image description here looks fine except xtic labels expect spaced out according value (i.e., "-9" shouldn't in middle of plot or "0" shouldn't right next "5", etc). in fact, since x-range (-10,10), want full range displayed on x-axis , colored bars positioned accordingly "number". 1 possible solution came fill in missing values (-10,10) using pandas assume there better/more obvious way deal this. haven't been able identify solution.

update:

thanks ajean's (and jd long's) responses below, using matplotlib code:

df_list = [df1, df2]  nrow = 2 ncol = 2 fig, axs = plt.subplots(nrow,ncol,figsize=(20,15))  in range(nrow*ncol):     #convert 1d 2d     row = / ncol     col = % ncol     if >= len(df_list):         axs[row,col].axis('off')     else:         axs[row,col].bar(np.array(df_list[i].index)-0.5, df_list[i]['a'], width=1, color='green')         axs[row,col].bar(np.array(df_list[i].index)-0.5, df_list[i]['b'], width=1, color='yellow')         axs[row,col].set_xlim([-10,10])         axs[row,col].set_ylim([0,20])         axs[row,col].xaxis.set_ticks(np.arange(-10, 11, 1)) 

which produces (wanted) result:

enter image description here

note: width of each bar set 1.0 , have been shifted -0.5 in order center each bar above tic marks.

it looks pandas has not (yet) given bar chart wrapper functionality capability explicitly place bar locations. 0.14.0 "what's new" indicates "coordinates of bar plots located on integer values (0.0, 1.0, 2.0 ...)", , nothing has changed through 0.15.1, far can tell.

therefore, i'd skip pandas interface (which using) , use matplotlib directly.

nrow = 1 ncol = 2 fig, axs = plt.subplots(nrow,ncol)  in range(nrow*ncol):     if >= len(df_list):         axs[i].axis('off')     else:         # theoretically turn loop on columns         # appropriate widths , offsets         axs[i].bar(df_list[i].index-0.4, df_list[i]['a'], width=0.4, color='green')         axs[i].bar(df_list[i].index, df_list[i]['b'], width=0.4, color='yellow') 

the above code changes defined dataframe list produces plot below (for simplicity eliminated axes).

bar chart @ exact locations

note: operation df_list[i].index-0.4 under pandas 0.14.0 produces error, bug has been fixed in 0.15.1. can around converting index normal numpy array first, or upgrade pandas.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -