我是靠谱客的博主 活力发带,最近开发中收集的这篇文章主要介绍matplotlib绘制简单密度图与直方图结合matplotlib绘制简单密度图与直方图结合,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
matplotlib绘制简单密度图与直方图结合
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib.mlab as mlab
# 加载数据
data = pd.read_csv("birth-rate.csv")
# 删除空数据
data.dropna(subset=['2008'], inplace=True)
# print(data.head(5))
#绘制密度图
k = mlab.GaussianKDE(data['2008'])
x = np.linspace(start=data['2008'].min(), stop=data['2008'].max(), num=100)
plt.plot(x, k(x))
# 绘制直方图
plt.hist(x=data['2008'],bins=np.arange(data['2008'].min(), data['2008'].max(), 3),density=True, edgecolor='black')
# 显示
plt.show()
结果图
数据
"""
0 11.716000
1 46.538000
2 42.875000
3 14.649000
4 13.281000
5 26.324048
6 14.004000
7 17.269000
8 15.299000
11 13.800000
12 9.326000
13 17.800000
14 34.465000
15 11.672000
16 39.395000
17 47.212000
18 21.431000
19 10.194000
20 18.017000
21 16.704000
22 9.097000
23 11.140000
24 24.698000
25 12.482000
26 27.104000
27 16.194000
28 11.208000
29 19.802000
30 21.494000
31 24.540000
32 35.422000
33 11.250000
34 10.055000
35 9.330000
36 14.942000
37 12.140000
38 34.952000
39 36.860000
40 34.509000
41 20.403000
42 32.426000
43 24.121000
44 16.677000
45 10.488000
47 11.496000
48 11.470000
49 8.312000
50 28.435000
52 11.839000
53 22.527000
54 20.759000
55 14.392480
56 13.860763
57 14.662983
58 12.611785
59 20.800000
60 24.698000
61 10.483857
62 36.983000
63 11.391000
64 11.955000
65 38.230000
66 10.857797
67 11.204000
68 20.945000
69 12.862000
71 25.256000
72 27.273000
73 12.935000
74 12.087000
75 32.355000
77 39.633000
78 36.761000
79 41.188000
80 37.971000
81 10.278000
82 19.419000
83 14.798445
84 33.009000
85 18.311000
86 17.869000
87 11.993008
88 11.300000
89 27.484000
90 39.306400
91 9.900000
92 27.643000
93 9.882000
94 18.569000
95 22.800000
96 16.906000
97 18.906000
98 31.220000
99 15.232000
100 21.500000
101 9.624000
102 16.690000
103 25.726000
104 8.700000
105 22.700000
106 38.767000
107 24.360000
108 24.733000
111 9.400000
112 19.010000
113 17.705000
114 18.771113
115 27.281000
116 15.727000
117 38.327000
118 23.299000
120 18.704826
121 34.624376
122 33.865294
123 9.900000
124 18.800000
125 20.035432
126 21.570196
127 28.942000
128 10.442000
129 11.452000
130 10.568000
131 8.204000
132 20.423000
134 12.320000
135 35.896000
136 18.709000
137 23.707650
138 18.328064
140 19.431061
141 10.898000
142 42.610000
143 10.018000
144 20.528000
145 24.054897
146 12.117000
147 18.785000
149 39.192000
150 33.587000
151 12.900000
152 40.224000
153 20.376000
154 25.081000
155 13.999705
156 27.555000
157 16.200000
158 53.536000
159 39.826000
160 24.624000
161 11.229000
162 14.910585
163 12.688000
164 25.385000
165 15.060000
166 11.735468
167 12.750821
168 21.961000
169 30.086000
170 20.640000
171 21.107000
172 24.728000
174 31.427000
175 10.872000
176 11.822378
177 13.732000
178 9.847000
179 24.625000
180 17.986000
181 12.075000
182 12.100000
183 41.132000
184 23.882067
185 23.424000
186 31.293000
187 38.435000
188 10.200000
189 30.447000
190 40.305000
191 20.235000
192 11.000000
193 44.105000
194 9.400000
195 38.455059
196 38.454670
197 32.110000
198 18.983000
199 10.609000
200 10.495000
201 11.855000
202 29.895000
203 17.800000
204 27.980000
206 45.692000
207 32.875000
208 14.520000
209 28.079000
210 21.933000
211 27.733000
212 14.830000
213 17.700000
214 18.229000
216 41.534000
217 46.151000
218 11.000000
219 17.137045
220 14.580000
221 14.300958
222 21.680535
223 17.581000
224 21.244000
225 12.000000
226 17.152000
227 30.198000
228 19.983917
229 23.508000
230 36.795000
231 22.038000
232 42.879000"""
最后
以上就是活力发带为你收集整理的matplotlib绘制简单密度图与直方图结合matplotlib绘制简单密度图与直方图结合的全部内容,希望文章能够帮你解决matplotlib绘制简单密度图与直方图结合matplotlib绘制简单密度图与直方图结合所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复