Python draws histogram and beautifies it

python drawing collection

往期绘图合集
python draws a simple line chart
python reads data in excel and draws multiple subgraphs and multiple groups of graphs on one canvas
python draws a histogram with error bars
python draws multiple subgraphs and displays them separately
python reads excel data and draws multiple y axis image


本期讲一下python绘制柱状图并使用不同颜色给柱子上色,设置柱子标签字体颜色


Preface

Histogram is a common way of visualizing data. It is usually used to show the comparative relationship between data of different categories or different time points. Through histograms, we can visually see the differences in data between different categories or different time points, helping us better understand the data.

In Python, you can use the Matplotlib library to draw histograms. Matplotlib is a simple and powerful Python drawing library that can draw various types of charts, including bar charts, line charts, scatter charts, and more.

Before drawing a histogram, we need to prepare the data. Normally, we would store the data in a NumPy array and then use the bar function in Matplotlib to plot it.


提示:以下是本篇文章正文内容,下面案例可供参考

1. Simple creation of histogram

import matplotlib.pyplot as plt
import numpy as np

# 生成实验数据
x = np.array(['A', 'B', 'C', 'D', 'E'])
y = np.array([12, 28, 19, 23, 20])

# 绘制柱状图
fig, ax = plt.subplots()
ax.bar(x, y)

# 显示图表
plt.show()

Insert image description here
In the above code, we first use the NumPy library to generate a set of experimental data, and then use the bar function in Matplotlib to draw a basic histogram. where x is the label of the column and y is the height of the column.

Next, we beautify the histogram to make it more beautiful. We can modify the color of the columns, add titles and labels, adjust font style and size, and more.

2. Beautification

1. Import the library

import matplotlib.pyplot as plt
import numpy as np

# 生成实验数据
x = np.array(['A', 'B', 'C', 'D', 'E'])
y = np.array([12, 28, 19, 23, 20])

# 设置字体样式和大小
plt.rcParams['font.family'] = ['Times New Roman']  # 中文字体为楷体,英文字体为新罗马字体
plt.rcParams['font.size'] = 24  # 坐标轴字号为16


# 绘制柱状图,设置柱子颜色和标签
fig, ax = plt.subplots(figsize=(10, 6))
bar_plot = ax.bar(x, y, width=0.5, color=['#00BFFF', '#FF8C00', '#32CD32', '#FF69B4', '#BA55D3'])

# 添加标题、标签和图例
ax.set_title('Tempreture/℃', fontsize=24)  # 标题字号为20
ax.set_xlabel('city', fontsize='28')
ax.set_ylabel('value', fontsize='28')
ax.legend(bar_plot, labels=x, loc='best')

# 调整字体颜色、柱子宽度等其他参数
for rect in bar_plot:
    height = rect.get_height()
    ax.text(rect.get_x() + rect.get_width()/2., height+1, '%d' % int(height),
            ha='center', va='bottom', fontsize=20, color='blue',fontname='Times New Roman')  # 字体颜色蓝色
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.tick_params(axis='x', direction='out', length=6, width=2)
ax.tick_params(axis='y', direction='out', length=6, width=2)

# 显示图表
plt.show()

2. Code interpretation

In the above code, you can make the bar chart more beautiful by modifying the font style and size, adjusting the column color, adding titles and labels, adjusting the font color, etc. Among them, we used New Roman font, blue font color, five different colors of columns, etc.

3. Operation results

Insert image description here

Guess you like

Origin blog.csdn.net/m0_58857684/article/details/130655315