mission details
The task of this level: Draw a graph containing a histogram and a line graph.
related information
In order to complete this task, you need to master: 1. The difference between histograms and bar graphs, 2. How to draw histograms.
- Applicable scenarios: Similar to histograms, it is suitable for displaying two-dimensional data sets and displaying the distribution of data. One axis represents the classification dimension that needs to be compared, and the other axis represents the corresponding value.
What is a histogram
In appearance, histograms and bar graphs look very similar. First, we need to distinguish the concepts clearly: histogram and bar chart.
-
A bar chart uses a long bar to represent each category. The length of the bar represents the frequency of the category, and the width represents the category.
-
A histogram is a statistical report chart, which is also in the form of long bars. However, the histogram uses the area of the long bar to represent the frequency, so the height of the long bar represents the frequency group interval, and the width represents the group interval. Its length and The widths are all meaningful. When the width is the same, the length of the bar is generally used to represent the frequency.
Draw a histogram
Histograms are generally used to describe equidistant data. Intuitively, the long bars of the histogram are connected together, indicating the mathematical relationship between the data.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
# 设置matplotlib正常显示中文和负号
matplotlib.rcParams['font.sans-serif']=['SimHei'] # 用黑体显示中文
matplotlib.rcParams['axes.unicode_minus']=False # 正常显示负号
# 随机生成(10000,)服从正态分布的数据
data = np.random.randn(10000)
plt.hist(data,bins=30, normed=0, facecolor="red", alpha=0.7)
# 显示横轴标签
plt.xlabel("区间")
# 显示纵轴标签
plt.ylabel("频数/频率")
# 显示图标题
plt.title("频数/频率分布直方图")
parameter | effect |
---|---|
data |
Required parameters, drawing data |
bins |
The number of bars in the histogram, optional, defaults to10 |
normed |
Whether to normalize the obtained histogram vector, optional, the default is 0 , which means no normalization and displays the frequency. normed=1 , represents the normalized, display frequency. |
facecolor |
Long strip of color |
edgecolor |
The color of the long border |
alpha |
transparency |
Programming requirements
Supplement the code in the editor on the right Begin-End
, draw the histogram and line graph in the same panel based on the input data, and set the histogram to red and the line graph to blue. The specific requirements are as follows:
-
Graphical
figsize
for(10, 10)
; -
The file name is
Task4/img/T1.png
.
Test instruction
The platform will test the code you write:
Expected output:你的答案与正确答案一致
import matplotlib
matplotlib.use("Agg")
import numpy as np
import matplotlib.pyplot as plt
def student(data,x,y):
'''
根据输入数据将直方图与线形图绘制在同一面板中,并设置直方图为红色,线形图为蓝色
:param data: 绘制直方图数据,类型为list
:param x,y: 绘制线形图数据,类型为list
:return: None
'''
# ********* Begin *********#
plt.figure(figsize=(10, 10))
# 绘制直方图
plt.hist(data, color='red')
# 绘制线形图
plt.plot(x, y, color='blue')
# 设置图例
# 保存图形
save_dir = 'Task4/img/'
plt.savefig(f'{save_dir}T1.png')
plt.close()
# ********* End *********#