matplotlib绘制简单密度图
程序员文章站
2022-05-22 13:09:20
...
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.show()