期望通过pthon语句实现直方图绘制,对比两个数据的分布。
data1=[3.415329266703285,3.450950593677052,2.9900107023219835,3.013844554717446,3.1981283588999365,3.1636275065184267,3.8707087206350215,4.037293276441531, 4.09018642606911,4.059165431464948, 3.9941067837502784,3.299489809046243,3.866450697991636,3.32432293858464, 3.302306163880027, 3.2023650635116536,3.1773325290249352, 3.9534357209900377, 3.39850952624823,3.3054586671141424]
data2=[4.035692505630228,3.28117341815394,3.946648451534541,3.1718928102948243,3.0666680615938864,3.5003444116258047,3.8568879942253966,4.078408145342986,3.1040795737222973,3.0322244310077044,3.560775898592889,3.0915179119649303,3.5077107919553465,3.1212776230255463,3.092151516339392,3.227259673469118,3.304800901718589,3.800294988550231, 3.354754685517257,3.236977911571232,3.199840308515418, 3.465511361978202,3.482746186560257, 3.3751607369131347, 3.494753925528949,3.41425160174232,3.390575467380132,3.119200057707103,4.076781819033243, 3.1632397948938364, 3.23609764994816]
希望横坐标能在(2.7,4)范围内,纵坐标表示的是在两个数据集中各自的分布频率(例如data1中在2.7~2.9的数据占data1总量的多少),两个data用不同颜色表示。
你可以使用Python中的matplotlib库来绘制直方图,并使用numpy库来计算数据的分布。以下是一个可能的代码示例:
import matplotlib.pyplot as plt
import numpy as np
# 设置横坐标范围
x = np.linspace(2.7, 4, 100)
# 计算数据的分布
hist1 = np.histogram2d(data1, data2, bins=range(len(data1)))
hist2 = np.histogram2d(data2, data1, bins=range(len(data2)))
# 绘制直方图
plt.hist2d(data1, data2, bins=range(len(data1)), cmap='Blues')
plt.xlim([2.7, 4])
# 计算每个数据点在两个数据集中的分布频率
freq1 = np.bincount(hist1[:, 0])/float(len(hist1))
freq2 = np.bincount(hist2[:, 0])/float(len(hist2))
# 绘制每个数据点的分布图
plt.hist(data1[:, 0], range=(0, len(data1)), color='r', alpha=0.5)
plt.hist(data2[:, 0], range=(0, len(data2)), color='b', alpha=0.5)
# 设置横坐标范围
plt.xlim([2.7, 4])
# 设置分布颜色
plt.xlabel('Value')
plt.ylabel('Frequency')
# 设置分布标签
plt.title('Distribution of Data1 and Data2')
# 显示图形
plt.show()
这段代码将绘制两个数据集的直方图,并将每个数据点的分布频率以不同颜色绘制在图中。你可以根据需要修改代码以适应你的具体要求。