Python group of data using a random histogrammed

0 Environment

  • python: × 64 bit 3.6.4
  • 包: matplotlib、pandas、numpy

A practical operation

 If it is a set of data to draw its histogram, this time, we have neither the only value of this set of data, and no number of unique values appeared. Then we first need to get these unique values, as well as the number of times they appear.
 If it is a small data sample (w level or less), then do not worry, you can make use of statistics for circulation. However, when the data sample is too large, for statistical efficiency of the cycle is not so high. At this point, we can use certain functions of pandas to achieve an efficient statistical effect, easy to draw the histogram. Specific operation is as follows:

  • 1 unique value and number of occurrences statistics
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 模拟100个[1, 10]之间的整数,用来绘制直方图
data = [np.random.randint(1, 10) for a in range(1, 101)]
# 将列表转换成pandas的Series格式(因为里边有自带的函数可以统计元素出现的个数, for统计列表中元素出现的次数,效率过慢,不适用于大数据量)
data_pd = pd.Series(data)
l_unique_data = list(data_pd.value_counts().index)  # 该数据中的唯一值
l_num = list(data_pd.value_counts())  # 唯一值出现的次数
  • 2 Draw Histogram
plt.bar(l_unique_data, l_num, width=0.1)
plt.show()
  • 3 Add the label
    if the label need to map some point, you can:
plt.bar(l_unique_data, l_num, width=0.1)
plt.plot(5, l_num[l_unique_data.index(5)], 'rs', label='标注')  # 标注点位
show = '({}, '.format(5) + str(l_num[l_unique_data.index(5)]) + ')'  # 注释的str
plt.annotate(show, xy=(5, l_num[l_unique_data.index(5)]))  # 进行注释
plt.ylabel('x轴')
plt.xlabel('y轴')
plt.legend()
plt.show()
  • 4 Initialization plt drawing format

Format initialization of the image can help in what circumstances to you: Character axes, marked character may be time Chinese characters need to convert the font formatting; in article writing, when some pictures being fixed ratio requirements; Axis font is too When I was young.

plt.rcParams['font.sans-serif'] = ['FangSong']  # 用来显示中文字符
plt.rcParams['figure.figsize'] = (6.6, 5)  # 控制图片的比例
plt.tick_params(labelsize=15)   # 坐标轴的字体大小

The results show:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_40260867/article/details/93625818