环境和包:
环境
python:python-3.12.0-amd64包:
matplotlib 3.8.2
代码:
#https://blog.csdn.net/aobulaien001/article/details/134611729
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import random
class TestPltText():
def __init__(self):
super(TestPltText, self).__init__()
# 读取excel数据
self.data = "plt_text.xlsx"
self.data_excel = pd.DataFrame(pd.read_excel(self.data))
# 获取相关内容
self.data_content = self.data_excel[["月份", "销量"]]
self.data_content01 = self.data_content.sort_values("销量", ascending=True)
self.data_content01 = self.data_content.sort_values("销量", ascending=True)
def test_plt_text(self):
# 设置窗口大小
plt.figure(figsize=(5, 4))
# 构造x,y轴坐标
y = np.array(list(self.data_content01["销量"]))
x_ticks = list(self.data_content01["月份"])
x = range(len(x_ticks))
# 绘制柱形图
plt.bar(x, y, width=0.5, align="center", color="b", alpha=0.6)
plt.xticks(range(len(x_ticks)), x_ticks, fontsize=6, rotation=90)
# x、y轴标签
plt.xlabel('月份')
plt.ylabel('销量')
plt.title('月销量(万)')
# 设置标签
for label1, label2 in zip(x, y):
plt.text(label1, label2+10,
'%.0f' % label2,
ha='center',
va='bottom',
fontsize=9)
# 设置y轴的范围
plt.ylim(0, 2600)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.show()
if __name__ == "__main__":
plt_text = TestPltText()
plt_text.test_plt_text()
效果图:
资源下载(分享-->资源分享):
链接:https://pan.baidu.com/s/1UlP0lsma8OWchfV5kstEFQ
提取码:kdgr