-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestScatter.py
More file actions
90 lines (78 loc) · 2.35 KB
/
testScatter.py
File metadata and controls
90 lines (78 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# !/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@Description : 散点图
@Author : Anttu
@Version : v1.0
@File : testScatter.py
@CreateTime : 15/5/2021 21:28
"""
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import os
import platform
# 系统
print(platform.system())
# 相对路径
project_dir = os.path.abspath('.')
print(project_dir)
fileName = "scatter.csv"
outFileName = "request_scatter.jpg"
fontName = "SimHei"
if platform.system() == 'Windows':
print('Windows系统')
fontName = ["SimHei"]
elif platform.system() == 'Linux':
print('Linux系统')
fontName = ["SimHei"]
elif platform.system() == 'Darwin':
print('MacOS系统')
fontName = ["PingFang HK"]
else:
print('其他')
# Mac字体 PingFang HK
# win字体 SimHei
plt.rcParams['font.sans-serif'] = fontName
plt.rcParams['font.family'] = 'sans-serif'
# 解决负号'-'显示为方块的问题
plt.rcParams['axes.unicode_minus'] = False
filePath = os.sep.join([project_dir, fileName])
outPath = os.sep.join([project_dir, outFileName])
# 全路径
# filePath = "D:\\request.csv"
# outPath = "D:\\request.jpg"
def main():
# 使用python下pandas库读取csv文件
data = pd.read_csv(filePath, encoding='utf-8')
# 读取列名
height = data.loc[:, ['性别', '身高']]
weight = data.loc[:, ['性别', '体重']]
# 身高
male_height = height.loc[height['性别'] == 1]
female_height = height.loc[height['性别'] == 0]
# 体重
male_weight = weight.loc[weight['性别'] == 1]
female_weight = weight.loc[weight['性别'] == 0]
# 设置画布
plt.figure(dpi=100, figsize=(24, 32))
# 散点图
plt.scatter(male_height, male_weight, marker='x', color='b', alpha=0.5)
plt.scatter(female_height, female_weight, color='r', alpha=0.5)
# 显示图例
plt.legend(('Male', 'Female'))
# X坐标-横坐标标题
plt.xlabel(u'身高/Height(厘米)', size=24)
# Y坐标-纵坐标标题
plt.ylabel(u'体重/Weight(公斤)', size=24)
# 统计图的标题
plt.title(u"XX公司男女身高/体重分布", size=32)
# 网格
plt.grid(True)
# 在展示图片前可以将画出的曲线保存到自己路径下的文件夹中
plt.savefig(outPath)
# 显示图像
plt.show()
print("all picture is starting")
if __name__ == "__main__":
main()