import matplotlib.pyplot as plt

# 显示均价条形图
def average_price_bar(x, y, title):
    """
    绘制平均价格条形图,展示不同区域的平均价格趋势。

    参数
    ----------
    x : list
        区域名称列表。
    y : list
        对应区域的平均价格列表。
    title : str
        图表标题。

    返回值
    -------
    None
    """
    plt.figure() # 创建图形画布
    
    plt.bar(x, y, alpha=0.8) # 绘制条形图
    
    plt.xlabel('区域') # 设置x轴标签
    plt.ylabel('均价') # 设置y轴标签
    plt.title(title) # 设置图表标题
    
    # 为每一个条形图添加数值标签
    for i, j in enumerate(y):
        plt.text(i, j + 100, j, ha='center')
    
    plt.show() # 显示图表

# 调用示例
x = ['A', 'B', 'C', 'D']
y = [1000, 2000, 1500, 1800]
title = 'Average Price by Region'
average_price_bar(x, y, title)
Python绘制平均价格条形图 - 数据可视化教程

原文地址: https://www.cveoy.top/t/topic/9I8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录