matplotlib之hist详解

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值