hist及参数解读
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 25 18:48:52 2018
@author: wanping
"""
import numpy as np
import matplotlib
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv("D:/apaper/pic/s2.csv")
data.head()
songTi = matplotlib.font_manager.FontProperties(fname='C:\Windows\Fonts\simsun.ttc')
x = data.loc[:,'n_songs']
#normed=True是频率图,默认是频数图
plt.hist(x, bins=30, range=(0,100), normed=True,
weights=None, cumulative=False, bottom=None,
histtype=u'bar', align=u'left', orientation=u'vertical',
rwidth=0.8, log=False, color=None, label=None, stacked=False,
hold=None)
plt.xticks(fontproperties=songTi,fontsize=12)
plt.yticks(fontproperties=songTi,fontsize=12)
plt.xlabel('用户听歌数量(首)',fontproperties=songTi,fontsize=14)
plt.ylabel('人数占比(%)',fontproperties=songTi,fontsize=14)
# plt.legend(fontsize=12)
fig = plt.gcf()
fig.set_size_inches(7.2, 4.2)
fig.savefig('D:/apaper/pic/用户听歌数量2.png', dpi=100)
plt.show()
- 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
normed :normed=True是频率图,默认是频数图
range :筛选数据范围,默认是最小到最大的取值范围
histtype:hist柱子类型
orientation:水平或垂直方向
rwidth= :柱子与柱子之间的距离,默认是0
图片中文乱码问题解决以及字体选择
本次选择的是宋体
songTi = matplotlib.font_manager.FontProperties(fname='C:\Windows\Fonts\simsun.ttc')
- 1
字体选择中的字体路径查看:
打开控制面板——》找到“字体”——》选择自己想要设置的字体,右击属性查看字体路径
通过fontproperties设置字体,fontsize设置字体大小
plt.xticks(fontproperties=songTi,fontsize=12)
plt.yticks(fontproperties=songTi,fontsize=12)
plt.xlabel('用户听歌数量(首)',fontproperties=songTi,fontsize=14)
plt.ylabel('人数占比(%)',fontproperties=songTi,fontsize=14)
# plt.legend(fontsize=12)
- 1
- 2
- 3
- 4
- 5
图片尺寸输出
通过以下设置图片尺寸并保存
fig = plt.gcf()
fig.set_size_inches(7.2, 4.2)
fig.savefig('D:/apaper/pic/用户听歌数量2.png', dpi=100)
- 1
- 2
- 3