基于python的散点图实现.py
import ggplot as gp
import pandas as pd
crime=pd.read_csv("crimeRatesByState2005.csv")
plot=gp.ggplot(gp.aes(x='murder',y='burglary'),data=crime)#谋杀案,入室盗窃
points=gp.geom_point(color='red')
print(plot+points)
基于python散点图实现2.py
import ggplot as gp
import pandas as pd
crime=pd.read_csv("crimeRatesByState2005.csv")
# 去掉离群点
# crime=crime[crime.state!="United States"]
crime=crime[crime.state!="District of Columbia"]
plot=gp.ggplot(gp.aes(x='murder',y='burglary'),data=crime)#谋杀案,入室盗窃
points=gp.geom_point(color='red')
#统计变量关系并拟合回归
smooth=gp.stat_smooth(method='loess',color='red')#loess指定局部加权回归的平滑拟合,并通过span参数控制曲线平滑度
#通过method = 'gam'指定加性模型的平滑拟合
print(plot+points+smooth)
基于python散点图实现3.py
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
crime=pd.read_csv("crimeRatesByState2005.csv")
# crime=crime[crime.state!="United States"]
# 去掉离群点
crime=crime[crime.state!="District of Columbia"]
crime1=crime.drop(['state'],axis=1)#drop删除列
crime=crime.drop(['population'],axis=1)
g=sns.pairplot(crime,diag_kind="kde",kind="reg")#kde密度曲线,reg拟合曲线
# kind:用于控制非对角线上的图的类型,可选"scatter散点图"与"reg"
#将 kind 参数设置为 "reg" 会为非对角线上的散点图拟合出一条回归直线,更直观地显示变量之间的关系.
# diag_kind:控制对角线上的图的类型,可选"hist柱形图"与"kde抛物线"
plt.show()
气泡图.py
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
crime = pd.read_csv("crimeRatesByState2005.csv")
crime2 = crime[crime.state != "United States"]
crime2 = crime2[crime2.state != "District of Columbia"]
s = list(crime2.population/10000)
colors = np.random.rand(len(list(crime2.murder)))
cm = plt.cm.get_cmap()
plt.scatter(x=list(crime2.murder), y=list(crime2.burglary), s=s,c=colors,cmap=cm, linewidth=0.5, alpha=0.5)
plt.xlabel("murder")
plt.ylabel("burglary")
plt.show()