当前位置: 首页 > news >正文

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

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

 

2.实例2--Bar Label Demo

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

 

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

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

 

http://www.lryc.cn/news/172410.html

相关文章:

  • pycharm 中package, directory, sources root, resources root的区别
  • 【谢希尔 计算机网络】第2章 物理层
  • Eclipse工具使用技巧
  • python LeetCode 刷题记录 94
  • 滴滴可观测平台 Metrics 指标实时计算如何实现了又准又省?
  • 每天几道Java面试题:IO流(第五天)
  • js/axios/umi-request 根据后端返回的二进制流下载文件
  • 软件评测师之流水线
  • Linux系统编程——网络编程的学习
  • Vue中的ref 和$refs的使用
  • Hive【非交互式使用、三种参数配置方式】
  • 基于Yolov8的工业小目标缺陷检测(1)
  • Python文件操作和管理指南:打开、读取、写入和管理文件
  • WebGL 用鼠标控制物体旋转
  • Spring Boot魔法:简化Java应用的开发与部署
  • 参议院算法Java
  • 前端提交规范 ESLint + Prettier + husky + lint-staged
  • python实现命令tree的效果
  • Deformable DETR(2020 ICLR)
  • springboot01
  • 虚拟机中window/ubuntu系统如何联网?
  • 计算物理专题----随机游走实战
  • 《思维与智慧》简介及投稿邮箱
  • flask+python快速搭建
  • 基于微信小程序的美术馆预约平台设计与实现(源码+lw+部署文档+讲解等)
  • ruoyi-vue-pro yudao 项目商城 mall 模块启用及相关SQL脚本
  • default 和 delete 与默认构造函数 的使用
  • 【开发篇】一、热部署
  • 点云从入门到精通技术详解100篇-定子装配过程中基于深度学习的易变形材料的点云分割(下)
  • 谷歌浏览器关闭自动更新功能