This is the second day of my participation in the August Text Challenge.More challenges in August

Hello, everyone, I am the talented brother.

When drawing a bar chart with Matplotlib, you often need to display data on a bar chart, which we will briefly cover today.

PLT. Text method

In matplotlib versions prior to 3.4.0, the plt.text method was used to draw data labels. Plt. text, as the name indicates, can draw the specified text at any position of the image. Based on this, we only need to draw the corresponding value at the coordinate position of the corresponding data point to realize the data label display.

import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False

fig, ax = plt.subplots(figsize=(8.4), dpi=100)

y_data = [ 29.21.17.14, ]
x_data = ('China'.'the United States'.'Japan'.'Australia')

# Bar chart color
color = 'coral'

# histogram
bar = plt.bar(x_data, y_data, 0.5, color=colors[1],edgecolor='grey')

# set the title
ax.set_title('Tokyo Olympic Gold Medal Count - as of 2nd August',fontsize=14,y=1.05)
Set the axis title
ax.set_ylabel("",fontsize = 12,color = 'black',alpha = 0.7,rotation=360)
# Set the Y interval
ax.set_ylim(0.30)

# Show data tags
for a,b in zip(x_data, y_data):
    plt.text(a,b,
             b,
             ha='center', 
             va='bottom'.)# Border hide
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
Copy the code

For plt.text(x, y, s, fontdict=None, **kwargs)

X,y: position of text (label)

S: text for display

Ha: horizontal alignment (optional: ‘center’, ‘right’, ‘left’)

Va: vertical alignment (optional: ‘center’, ‘top’, ‘bottom’, ‘baseline’, ‘center_baseline’)

PLT. Bar_label method

In the latest version of Matplotlib (3.4.0 and later), we found that a function called plt.bar_label works well for displaying bar graphs (including bar graphs) data labels.

plt.bar_label(
    container,
    labels=None,
    *,
    fmt='%g',
    label_type='edge',
    padding=0,
    **kwargs,
)
Copy the code

Container: Returned value of bar or barh

Labels: list of label text, default to None, data for columns formatted with the FMT argument (column height)

FMT: The format string for the label, default ‘%g’, that is, the label value is formatted as a floating point number

Label_type: label type, which can be ‘edge’ or ‘center’. The default value is ‘edge’. For normal bar graphs, this parameter is used only to control the position of labels. For stacked bars, different label types correspond to different label values’ EDGE ‘: the label is located at the end of the column’ Center ‘: the label is located in the middle of the column

Padding: Distance between the label and the column, in pixels. Default is 0

In the original code, replace the part of the code that displays the data label

# Show data tags
plt.bar_label(bar, label_type='edge')
Copy the code

Official website reference:

Matplotlib.org/stable/api/…

The above is the simple content of this time, you can modify the parameters feel!

Finally, wish us more gold in this Olympic Games!