python怎么用matplotlib画柱状图

如题所述

Python——使用matplotlib绘制柱状图

1、基本柱状图

首先要安装matplotlib 可以使用pip命令直接安装

[python] view plain copy

    # -*- coding: utf-8 -*-  

    import matplotlib.pyplot as plt  

    num_list = [1.5,0.6,7.8,6]  

    plt.bar(range(len(num_list)), num_list)  

    plt.show()  

    2、设置颜色

    [python] view plain copy

    # -*- coding: utf-8 -*-  

    import matplotlib.pyplot as plt  

    num_list = [1.5,0.6,7.8,6]  

    plt.bar(range(len(num_list)), num_list,fc='r')  

    plt.show()  

    [cpp] view plain copy

    # -*- coding: utf-8 -*-  

    import matplotlib.pyplot as plt  

    num_list = [1.5,0.6,7.8,6]  

    plt.bar(range(len(num_list)), num_list,color='rgb')  

    plt.show()  

    3、设置标签

    [python] view plain copy

    # -*- coding: utf-8 -*-  

    import matplotlib.pyplot as plt  

    name_list = ['Monday','Tuesday','Friday','Sunday']  

    num_list = [1.5,0.6,7.8,6]  

    plt.bar(range(len(num_list)), num_list,color='rgb',tick_label=name_list)  

    plt.show()  

    4、堆叠柱状图

温馨提示:答案为网友推荐,仅供参考
相似回答