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 np

plt.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_count
    ax.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 = 0

fig, ax = plt.subplots(layout='constrained')
for attribute, measurement in data.items():
    offset = width * multiplier
    rects = ax.bar(x + offset, measurement, width, label=attribute)
    ax.bar_label(rects, padding=3)
    multiplier += 1


ax.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()

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

温柔济沧海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值