Experiment 2—U.S. crime rate data scatter plot drawing Educoder

1. Drawing a scatter diagram of crime rate data in the United States - the basic steps of drawing a scatter diagram

Task description
The task of this level: draw a scatter diagram based on the crime rate of each state in the United States in 2005 provided by the training, and save it as a PDF or png file.

Relevant knowledge
In order to complete the task of this level, you need to master:
the basic steps of scatterplot drawing;
scatterplot display optimization.

Programming Requirements
According to the prompts, supplement the code in the Begin-End interval of the editor on the right, filter out the Washington DC and national average data, and draw a scatter plot, where the x-axis and y-axis both start from 0, the points are represented by asterisks, and the colors are is "#00CC88".
Test instructions
The platform will test the code you write, if your graphics are consistent with the correct answer graphics, you will pass the level.
Image expected output example:
insert image description here
code display:

# -*- 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() #关闭画布窗口

2. Scatterplot drawing of US crime rate data - locally weighted regression

Task description
The task of this level: According to the crime rate data in the United States, integrate the knowledge of the previous level, realize local weighted regression, draw a scatter diagram and save it as a PDF or png file.

Relevant knowledge
In order to complete this task, you need to master:

Programming Requirements
According to the prompt, supplement the code in the Begin-End interval of the editor on the right, draw a scatter plot using the crime rate data in the United States, filter out the Washington DC and national average data, and draw a scatter plot, where the x-axis and y-axis are both from Starting from 0, the point is indicated by an asterisk, and the color is #00CC88. Perform local weighted regression on the data, and add a title to the picture. Note that the title is in bold Chinese characters, and the font size is 16. Save the picture in png format. Set the canvas size to 8.0*4.0.

Test instructions
The platform will test the code you write, if your graphics are consistent with the correct answer graphics, you will pass the level.
Image expected output example:
insert image description here
code display:

# -*- 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轴标签

    ax.set_title("美国谋杀率和入室盗窃率",fontproperties="SimHei", fontsize=16) #设置图像的标题

    #获取没有全美平均值和华盛顿特区的犯罪率数据
    crime2=crime[~crime['state'].isin(['District of Columbia','United States'])]   
     
    #对数据采用默认值进行平滑处理,返回值为一个二维数组
    lowess = sm.nonparametric.lowess(crime2["burglary"], crime2["murder"])  

    ax.plot(lowess[:, 0], lowess[:, 1]) #将二维数组绘制出来

    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_2/crime.png') #保存为png格式
    plt.close() #关闭画布窗口


Guess you like

Origin blog.csdn.net/qq_58529413/article/details/130290447