Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
GrowerCoder
PyDataPreprocessing
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
PyDataPreprocessing
/
Chapter10
/
histogram.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
65 lines (47 loc) · 2 KB
master
Breadcrumbs
PyDataPreprocessing
/
Chapter10
/
histogram.py
Copy path
Top
File metadata and controls
Code
Blame
65 lines (47 loc) · 2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# coding:utf8
"""
Description:matplotlib绘制直方图
Author:伏草惟存
Prompt: code in Python3 env
"""
'''
直方图定义:
直方图(英语:Histogram)是一种对数据分布情况的图形表示,是一种二维统计图表,它的两个坐标分别是统计样本和该样本对应的某个属性的度量。
优点:
可以很清晰地看出每个类的总和及各个属性的比例。
缺点:
不容易看出各个属性的频数。
应用场景
适合的场景:
适合应用到分类数据对比。比如一个游戏销量的图表,展示不同游戏类型的销量对比。 不适合的场景:
1 分类太多不适合使用纵向柱状图,如对比不同省份的人口数量。
2 不适合表示趋势。柱状图使用矩形的长度(宽度)来对比分类数据的大小,非常方便 临近的数据进行大小的对比,不适合展示连续数据的趋势。
'''
import
matplotlib
import
matplotlib
.
pyplot
as
plt
import
pygal
# Pygal生成可缩略的矢量图文件
#加入中文显示
import
matplotlib
.
font_manager
as
fm
# 解决中文乱码,本案例使用宋体字
myfont
=
fm
.
FontProperties
(
fname
=
r"C:\\Windows\\Fonts\\simsun.ttc"
)
def
histogram
(
xvalues
,
yvalues
):
# 绘制直方图
hist
=
pygal
.
Bar
()
# 设置散点图标题和横纵坐标标题
hist
.
title
=
'事件频率的直方图'
hist
.
x_title
=
'事件的结果'
hist
.
y_title
=
'事件的频率'
# 绘制气温图,设置图形大小
fig
=
plt
.
figure
(
dpi
=
100
,
figsize
=
(
10
,
6
))
# 事件的结果
hist
.
x_labels
=
xvalues
# 事件的统计频率
hist
.
add
(
'事件'
,
yvalues
)
# 保存文件路径
hist
.
render_to_file
(
'die_visual.svg'
)
if
__name__
==
'__main__'
:
xvalues
=
list
(
range
(
1
,
100
))
#校正坐标点,即横坐标值列表
yvalues
=
[
x
**
2
for
x
in
xvalues
]
# 纵坐标值列表
x_result
=
[
1
,
2
,
3
,
4
,
5
,
6
]
y_frequencies
=
[
152
,
171
,
175
,
168
,
150
,
179
]
histogram
(
x_result
,
y_frequencies
)
You can’t perform that action at this time.