# -*- coding: utf-8 -*-
import pandas as pd #用于生成满足绘图要求的数据格式
from matplotlib import pyplot as plt #用于绘制散点图
# import statsmodels.api as sm #用于局部加权回归
crime=pd.read_csv(r"matplotlibScatter/csv/crimeRatesByState2005.csv") #返回值为二维标记数据结构 DataFrame
def plot():
# ********* Begin *********#
fig,ax=plt.subplots() #subplots返回画布和子图
ax.set_xlabel("crime murder", fontsize=12) #设置x轴标签
ax.set_ylabel("crime burglary", fontsize=12) #设置y轴标签
#获取没有全美平均值和华盛顿特区的犯罪率数据
crime2=crime[~crime['state'].isin(['District of Columbia','United States'])]
ax.set_xlim(0,10) #x轴范围从0到10
ax.set_ylim(0,1200) #y轴范围从0到1200
ax.plot(crime2["murder"],crime2["burglary"],"*",color="#00CC88") #绘制散点图
plt.show() #展示图像
# ********* End *********#
plt.savefig('matplotlibScatter/studentanswer/level_1/crime.png') #保存为png格式
plt.close() #关闭画布窗口
10-07
6426
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-29
3440
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-11
2161
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-09
4013
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-28
1718
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)