• Python与数据分析--每天绘制Matplotlib库实例图片3张-第1天


    目录

    1.实例1--Bar color demo

    2.实例2--Bar Label Demo

    3.实例3--Grouped bar chart with labels

    1.实例1--Bar color demo

    1. import matplotlib.pyplot as plt
    2. # 支持中文
    3. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
    4. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    5. fig, ax = plt.subplots()
    6. fruits = ['苹果', '蓝莓', '樱桃', '橘子']
    7. counts = [40, 100, 30, 55]
    8. bar_labels = ['red', 'blue', 'pink', 'orange']
    9. bar_colors = ['tab:red', 'tab:blue', 'tab:pink', 'tab:orange']
    10. ax.bar(fruits, counts, label=bar_labels, color=bar_colors)
    11. ax.set_ylabel('水果供应数量')
    12. ax.set_title('水果供应数量图')
    13. ax.legend(title='Fruit color')
    14. plt.savefig(r"C:\Users\Zeng Zhong Yan\Desktop\py.vs\python学习\1.水果供应数量图.png",dpi=500)
    15. plt.show()

     

    2.实例2--Bar Label Demo

    1. import matplotlib.pyplot as plt
    2. import numpy as np
    3. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
    4. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    5. species = ('Adelie', 'Chinstrap', 'Gentoo')
    6. Male_data=np.array([73, 54, 61])
    7. Female_data=np.array([73, 34, 58])
    8. sex_counts = {'Male':Male_data,'Female':Female_data ,}
    9. width=0.6#条形图的宽度
    10. fig, ax = plt.subplots()
    11. bottom = np.zeros(3)
    12. for sex, sex_count in sex_counts.items():
    13. p = ax.bar(species, sex_count, width, label=sex,bottom=bottom)
    14. bottom += sex_count
    15. ax.bar_label(p, label_type='center')
    16. ax.set_title('企鹅性别数量统计图')
    17. ax.legend(edgecolor='y')
    18. plt.savefig(r"C:\Users\Zeng Zhong Yan\Desktop\py.vs\python学习\2.企鹅性别数量统计图.png",dpi=500)
    19. plt.show()

     

    3.实例3--Grouped bar chart with labels

    1. import matplotlib.pyplot as plt
    2. import numpy as np
    3. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
    4. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    5. x_label=[ "Adelie", "Chinstrap", "Gentoo"]
    6. data = {
    7. 'Bill Depth': (18.35, 18.43, 14.98),
    8. 'Bill Length': (38.79, 48.83, 47.50),
    9. 'Flipper Length': (189.95, 195.82, 217.19),
    10. }
    11. x = np.arange(len(x_label))
    12. width = 0.25
    13. multiplier = 0
    14. fig, ax = plt.subplots(layout='constrained')
    15. for attribute, measurement in data.items():
    16. offset = width * multiplier
    17. rects = ax.bar(x + offset, measurement, width, label=attribute)
    18. ax.bar_label(rects, padding=3)
    19. multiplier += 1
    20. ax.set_ylabel('长度(纳米)')
    21. ax.set_title('不同种类企鹅属性图')
    22. ax.set_xticks(x + width,x_label)
    23. ax.legend(loc='upper left', ncols=3)
    24. ax.set_ylim(0, 250)
    25. plt.savefig(r"C:\Users\Zeng Zhong Yan\Desktop\py.vs\python学习\3.不同种类企鹅属性图.png",dpi=500)
    26. plt.show()

     

  • 相关阅读:
    vue使用pcl.js展示.pcd/.bin文件代码源码
    opencv-python图像处理:阈值,滤波,腐蚀,膨胀,梯度
    游读广州|康园环保行
    Kubeadm搭建kubernetes集群
    聊一下C#中的lock
    torch加载glove预训练的embedding
    PostgreSQL JDBC连接详解(附DEMO)
    ELK日志系统
    记录一次Docker与Redis冲突
    社区街道治安智慧监管方案,AI算法赋能城市基层精细化治理
  • 原文地址:https://blog.csdn.net/m0_71819746/article/details/133137510