-1

I am using subplot in matplotlib to make bar graph currently showing data labels on top of the bars (in rotated position). Please see the image.

enter image description here

as per this the datalabels are touching the bars. Can they be offset to give some space between the bar and the datalabel. Following is my code. Please help.

x = df_graph3['Parameter2'].values
y1 = df_graph3['Vol Flow'].values

z1 = Figure(figsize=(2, 3), tight_layout=TRUE)
plot1 = z1.add_subplot(1, 1, 1)
plot1.bar(x, y1, color='#EB3F3F')
plot1.spines['top'].set_visible(False)
plot1.spines['right'].set_visible(False)
plot1.spines['left'].set_visible(False)
plot1.spines['bottom'].set_visible(False)

for i in range(len(x)):
    plot1.text(i, y1[i], y1[i], ha='center', va='bottom', size=8,rotation=90)

plot1.axes.get_yaxis().set_visible(False)
plot1.set_xticklabels(x, fontsize=8)

canvas1 = FigureCanvasTkAgg(z1, master=frame_dash3)
canvas1.draw()
canvas1.get_tk_widget().grid(row=5, column=0, columnspan=2, pady=10, sticky='nsew')
  • Are you talking about the int values like `88.64` and `8.74`? Can you just add, say, 1 to `y1[i]` ? Like `plot1.text(i, y1[i]+1, y1[i]+1, ha='center', va='bottom', size=8,rotation=90)`? – Michael S. Jul 22 '22 at 16:02
  • @MichaelS. I cant do it. Because it will change values of datalabels to be 88.64+1=89.64 and same with other datalabels – Muhammad Farzan Bashir Jul 22 '22 at 16:17
  • Whoops. Just add your value to the first `y[i]`. According to [the documentation](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.text.html), your first `y[i]` value is the text's position while the second `y[i]` value is the label. Like: `plot1.text(i, y1[i]+1, y1[i], ha='center', va='bottom', size=8,rotation=90)` – Michael S. Jul 22 '22 at 16:20
  • You should be using `matplotlib.pyplot.bar_label`, which has a `padding=` parameters – Trenton McKinney Jul 22 '22 at 21:30
  • The plot should be `ax = df.plot(kind='bar', x='Parameter2', y='Vol Flow', figsize=(2, 3))`, `ax.spines[['top', 'bottom', 'right', 'left']].set_visible(False)`, `ax.bar_label(ax.containers[0], label_type='edge', rotation=90, fontsize=7, padding=3)`, `ax.margins(y=0.1)`. – Trenton McKinney Jul 22 '22 at 21:34
  • Or assign df.plot to a specific existing subplot: `df.plot(kind='bar', x='Parameter2', y='Vol Flow', ax=plot1)` – Trenton McKinney Jul 22 '22 at 21:37

1 Answers1

2

I changed the y value of plot1.text(i, y[i]+1, ...) This will increase the location of the text to 1 point. you can increase the space with increasing the value in y[i]+your_value

x = df_graph3['Parameter2'].values
y1 = df_graph3['Vol Flow'].values

z1 = Figure(figsize=(2, 3), tight_layout=TRUE)
plot1 = z1.add_subplot(1, 1, 1)
plot1.bar(x, y1, color='#EB3F3F')
plot1.spines['top'].set_visible(False)
plot1.spines['right'].set_visible(False)
plot1.spines['left'].set_visible(False)
plot1.spines['bottom'].set_visible(False)

for i in range(len(x)):
    plot1.text(i, y1[i]+1, y1[i], ha='center', va='bottom', size=8,rotation=90)

plot1.axes.get_yaxis().set_visible(False)
plot1.set_xticklabels(x, fontsize=8)

canvas1 = FigureCanvasTkAgg(z1, master=frame_dash3)
canvas1.draw()
canvas1.get_tk_widget().grid(row=5, column=0, columnspan=2, pady=10, sticky='nsew')

Hope this'll work :)