Search code examples
python-3.xpandasdataframesubplot

python how do I add bar values to my dataframe subplots bar charts?


I have four dataframes as follow and want to add bar values to my four dataframe subplots' bar charts. How do I add bar values to my dataframe subplots bar charts?

# create two dataframes
data1 = {'column1': [10.123456],
        'column2': [2.345678]}
data2 = {'column1': [50.123456],
        'column2': [5.345678]}
data3 = {'column1': [100.123456],
        'column2': [10.345678]}
data4 = {'column1': [150.123456],
        'column2': [12.345678]}
# Creates pandas DataFrame.
df1 = pd.DataFrame(data1, index=[100])
df2 = pd.DataFrame(data2, index=[1000])
df3 = pd.DataFrame(data3, index=[10000])
df4 = pd.DataFrame(data4, index=[100000])
print(df1)
print(df2)
print(df3)
print(df4)

# create subplots for each dataframe
fig, axes = plt.subplots(figsize=(15,8), nrows=1, ncols=4);

# plot the PostgreSQL15 and SQLite3 database creation comparison times
df1[["column1", "column2"]].plot(ax=axes[0], rot=0, kind='bar', width=0.4, edgecolor='white', linewidth=5, 
                                      color=["blue", "green"], legend=None, fontsize=12);
df2[["column1", "column2"]].plot(ax=axes[1], rot=0, kind='bar', width=0.4, edgecolor='white', linewidth=5, 
                                      color=["blue", "green"], legend=None, fontsize=12);
df3[["column1", "column2"]].plot(ax=axes[2], rot=0, kind='bar', width=0.4, edgecolor='white', linewidth=5, 
                                      color=["blue", "green"], legend=None, fontsize=12);
df4[["column1", "column2"]].plot(ax=axes[3], rot=0, kind='bar', width=0.4, edgecolor='white', linewidth=5, 
                                      color=["blue", "green"], legend=None, fontsize=12);

enter image description here


Solution

  • we can use the annotate function like this:

    fig, axes = plt.subplots(figsize=(15,8), nrows=1, ncols=4)
    
    # plot the bar charts
    df1[["column1", "column2"]].plot(ax=axes[0], rot=0, kind='bar', width=0.4, edgecolor='white', linewidth=5, 
                                          color=["blue", "green"], legend=None, fontsize=12)
    df2[["column1", "column2"]].plot(ax=axes[1], rot=0, kind='bar', width=0.4, edgecolor='white', linewidth=5, 
                                          color=["blue", "green"], legend=None, fontsize=12)
    df3[["column1", "column2"]].plot(ax=axes[2], rot=0, kind='bar', width=0.4, edgecolor='white', linewidth=5, 
                                          color=["blue", "green"], legend=None, fontsize=12)
    df4[["column1", "column2"]].plot(ax=axes[3], rot=0, kind='bar', width=0.4, edgecolor='white', linewidth=5, 
                                          color=["blue", "green"], legend=None, fontsize=12)
    
    # add bar values to the plots
    for ax in axes:
        for p in ax.patches:
            ax.annotate(format(p.get_height(), '.2f'), (p.get_x() + p.get_width() / 2., p.get_height()), 
                        ha = 'center', va = 'center', xytext = (0, 10), textcoords = 'offset points')
    
    plt.show()
    

    enter image description here