1. histogram drawing --hish () Draw
import matplotlib.pyplot as plt
import numpy as np
import math
#### 创建画布
plt.figure(figsize=(20,8))
###创建数据
age = [10,20,30,44,23,32,55,11,8,9,17,19,20,33,30,30,40,44,55,55,21,66,64,61,60,66,73,84,73,83,88]
###指定分组情况-----默认系统自动进行分组
# bins = 6
##### 自定义分组
###找到最大值,最小值相减,得出极差
max = np.max(age)
min = np.min(age)
##极差
ptp = max-min
##指定组距----10为一组
bins = math.ceil(ptp/5)
arr = np.arange(min,(5*bins)+6,5)
print(arr)
###绘图
plt.hist(age,bins=bins,color='g')
plt.title('年龄直方图')
##默认不支持中文,想用中文,必须进行设置RC参数
plt.rcParams['font.sans-serif'] = 'SimHei'
##默认不支持负号,想用负号,进行RC参数设置
plt.rcParams['axes.unicode_minus'] = False
##x轴刻度
plt.xticks(arr)
###保存
plt.savefig('./直方图.png')
##展示
plt.show()
2. boxplot --boxplot () plotted as a function
boxplot function
matplotlib.pyplot.boxplot(x, notch=None, sym=None, vert=None, whis=None, positions=None,
widths=None, patch_artist=None,meanline=None, labels=None, … )
Common parameters
parameter name | Explanation | parameter name | Explanation |
x | Receiving array. It represents a number of draw box plot data. No default. |
positions | Receiving array. Graphical representation position. The default is None. |
notch | Receiving boolean. Middle box indicating whether there are missing mouth. The default is None. |
widths | Receiving scalar or array. Each box represents a width. The default is None. |
sym | Receiving a specific sting. Dot shape specified exception. Farmer believes None. |
labels | Receiving array. Each specified boxplot marked tab. The default is None. |
vert | Receiving boolean. It is a graph showing the lateral longitudinal or by a transverse. The default is None. |
meanline | Receiving boolean. Indicating whether the average line. The default is False. |
import matplotlib.pyplot as plt
import numpy as np
data = np.load('./国民经济核算季度数据.npz')
for tmp in data:
print(tmp)
columns = data["columns"]
values = data["values"]
print(columns)
print(values)
##创建画布
fig = plt.figure(figsize=(10,5))
##默认不支持中文,想用中文,必须进行设置RC参数
plt.rcParams['font.sans-serif'] = 'SimHei'
##默认不支持负号,想用负号,进行RC参数设置
plt.rcParams['axes.unicode_minus'] = False
###第一个子画布
fig.add_subplot(2,1,1)
x = values[:,3:6]
label = ["第一产业", "第二产业", "第三产业"]
##notch----缺口
##meanline----中线
plt.boxplot(x,notch=False,showmeans=True,meanline=True,labels=label)
plt.xlabel('产业')
plt.ylabel('各产业生产总值(亿元)')
plt.title('2000-2017年各产业生产总值箱线图')
##第二个子画布
fig.add_subplot(2,1,2)
x = values[:,6::]
label = ["农林业", "工业", "建筑业", "批发业", "交通业", "餐饮业", "金融业", "房地产业", "其他"]
plt.boxplot(x,notch=False,meanline=True,labels=label)
plt.xlabel('行业')
plt.ylabel('各行业生产总值(亿元)')
plt.title('2000-2017年各行业生产总值箱线图')
plt.show()