学校的作业鸭

这篇博客展示了学校的数学建模作业,重点介绍了如何使用Python和Matlab进行数据可视化,包括绘制条形图、散点图、二维直方图和正方形矩阵图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可视化案例(今日作业)

条形图

# 图书馆
import matplotlib.pyplot as plt
 
# 创建条形图
barWidth = 0.9
bars1 = [3, 3, 1]
bars2 = [4, 2, 3]
bars3 = [4, 6, 7, 10, 4, 4]
bars4 = bars1 + bars2 + bars3
 
# X轴的位置
r1 = [1,5,9]
r2 = [2,6,10]
r3 = [3,4,7,8,11,12]
r4 = r1 + r2 + r3
 
# 创建条形图
plt.bar(r1, bars1, width = barWidth, color = (0.3,0.1,0.4,0.6), label='Alone')
plt.bar(r2, bars2, width = barWidth, color = (0.3,0.5,0.4,0.6), label='With Himself')
plt.bar(r3, bars3, width = barWidth, color = (0.3,0.9,0.4,0.6), label='With other genotype')
# 

注意:可以很容易地创建条形图。有关其他示例,请参见条形图部分。 
 
# 创建图例
plt.legend()
 
# 以90°旋转的每个条形图下方的文本
plt.xticks([r + barWidth for r in range(len(r4))], ['DD', 'with himself', 'with DC', 'with Silur', 'DC', 'with himself', 'with DD', 'with Silur', 'Silur', 'with himself', 'with DD', 'with DC'], rotation=90)
 
# Create labels
label = ['n = 6', 'n = 25', 'n = 13', 'n = 36', 'n = 30', 'n = 11', 'n = 16', 'n = 37', 'n = 14', 'n = 4', 'n = 31', 'n = 34']
 
# 每个栏顶部的文本
for i in range(len(r4)):
    plt.text(x = r4[i]-0.5 , y = bars4[i]+0.1, s = label[i], size = 6)

# 调整页边距
plt.subplots_adjust(bottom= 0.2, top = 0.98)
 
# 显示图形
plt.show()

在这里插入图片描述

# 图书馆
import numpy as np
import matplotlib.pyplot as plt
 
# 设置宽度
barWidth = 0.25
 
# set heights of bars
bars1 = [12, 30, 1, 8, 22]
bars2 = [28, 6, 16, 5, 10]
bars3 = [29, 3, 24, 25, 17]
 
# 设置高度
r1 = np.arange(len(bars1))
r2 = [x + barWidth for x in r1]
r3 = [x + barWidth for x in r2]
 
# 绘制绘图
plt.bar(r1, bars1, color='#7f6d5f', width=barWidth, edgecolor='white', label='var1')
plt.bar(r2, bars2, color='#557f2d', width=barWidth, edgecolor='white', label='var2')
plt.bar(r3, bars3, color='#2d7f5e', width=barWidth, edgecolor='white', label='var3')
 
# 在组条形图的中间添加xticks
plt.xlabel('group', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))], ['A', 'B', 'C', 'D', 'E'])
 
# 创建图例并显示图形
plt.legend()
plt.show()

在这里插入图片描述

# 图书馆
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
import pandas as pd
 
# y轴粗体
rc('font', weight='bold')
 
# 每组的值 
bars1 = [12, 28, 1, 8, 22]
bars2 = [28, 7, 16, 4, 10]
bars3 = [25, 3, 23, 25, 17]
 
# bars1 + bars2的高度
bars = np.add(bars1, bars2).tolist()
 
# 在x轴上的位置
r = [0,1,2,3,4]
 
# 组名称和条形宽度
names = ['A','B','C','D','E']
barWidth = 1
 
# 创建棕色条形图
plt.bar(r, bars1, color='#7f6d5f', edgecolor='white', width
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值