关于matplotlib绘制直方图偏移的问题
在使用pyplot绘制直方图的时候我发现了一个问题,在给函数.hist()传参的时候,如果传入的组数不是刚刚好(就是说这个组数如果是使用(最大值-最小值)/组距计算出来,而这个数字不是整除得来而是取整得来的话),图像就会产生偏移现象。
看下面这段代码:绘制IMDB排行前1000电影的时长分布直方图
# coding=utf-8
from matplotlib import pyplot as plt
import pandas as pd
# 数据准备
file_path = "./IMDB-Movie-Data.csv"
df = pd.read_csv(file_path)
runtime_data = df["Runtime (Minutes)"]
# 计算组数
max_runtime = max(runtime_data)
min_runtime = min(runtime_data)
num_bin = int((max_runtime-min_runtime)//6)
# 配置图形参数
plt.figure(figsize=(20, 8), dpi=80)
plt.grid(alpha=0.5)
# 绘图
plt.hist(runtime_data, num_bin)
plt.xticks(range(min_runtime, max_runtime+6, 6))
plt.show()
结果如下:

- 产生这个问题的原因就在于,在这个程序中
max_runtime-min_runtime的值是125,不能被6整除,所以产生了偏移。
如果我们将上述代码中的(max_runtime-min_runtime)//6和plt.xticks(range(min_runtime, max_runtime+6, 6))中的6更换为一个能被125整除的数,比如5,结果会是如何呢?

- 我们可以看到问题解决了,偏移消失了,但是这并不是解决问题的根本办法,如果我们就是要用6作为组距而不想偏移呢?
我们可以传入一个列表参数来解决这个问题
# coding=utf-8
from matplotlib import pyplot as plt
import pandas as pd
# 数据准备
file_path = "./IMDB-Movie-Data.csv"
df = pd.read_csv(file_path)
runtime_data = df["Runtime (Minutes)"]
# 将传参从组数改为传入列表
max_runtime = max(runtime_data)
min_runtime = min(runtime_data)
plt.figure(figsize=(20, 8), dpi=80)
plt.hist(runtime_data, range(min_runtime, max_runtime+6, 6))
plt.grid(alpha=0.5)
plt.xticks(range(min_runtime, max_runtime+6, 6))
plt.show()
结果如图:

关于matplotlib绘制直方图偏移的问题的更多相关文章
- NumPy使用 Matplotlib 绘制直方图
NumPy - 使用 Matplotlib 绘制直方图 NumPy 有一个numpy.histogram()函数,它是数据的频率分布的图形表示. 水平尺寸相等的矩形对应于类间隔,称为bin,变量hei ...
- matplotlib绘制直方图【柱状图】
代码: def drawBar(): xticks = ['A', 'B', 'C', 'D', 'E']#每个柱的下标说明 gradeGroup = {'A':200,'B':250,'C':330 ...
- Python:matplotlib绘制直方图
使用hist方法来绘制直方图: 绘制直方图,最主要的是一个数据集data和需要划分的区间数量bins,另外你也可以设置一些颜色.类型参数: plt.hist(np.random.randn(1 ...
- numpy和matplotlib绘制直方图
使用 Matplotlib Matplotlib 中有直方图绘制函数:matplotlib.pyplot.hist()它可以直接统计并绘制直方图.你应该使用函数 calcHist() 或 np.his ...
- 4.matplotlib绘制直方图
  # coding=utf-8 from matplotlib import pyplot as plt from matplotlib import font_manager a=[131, ...
- 利用pandas读取Excel表格,用matplotlib.pyplot绘制直方图、折线图、饼图
利用pandas读取Excel表格,用matplotlib.pyplot绘制直方图.折线图.饼图 数据: 折线图代码: import pandas as pdimport matplotlib. ...
- matplotlib如何绘制直方图、条形图和饼图
1 绘制直方图: import matplotlib.pyplot as plt import numpy as np import matplotlib def hist1(): # 设置matpl ...
- python包matplotlib绘制图像
使用matplotlib绘制图像 import matplotlib.pyplot as plt from matplotlib.pyplot import MultipleLocator impor ...
- matplotlib绘制动画
matplotlib从1.1.0版本以后就开始支持绘制动画,具体使用可以参考官方帮助文档.下面是一个很基本的例子: """ A simple example of an ...
随机推荐
- CListCtrl颜色设置
动态改变listctrl 单元格背景及文字颜色 m_listshow.InsertColumn( 0, "ID", LVCFMT_LEFT, 40 );//插入列 m_listsh ...
- Eclipse中STM32工程建立步骤
前段时间一直在折腾linux系统上STM32的开发,网上一顿搜,费劲九牛二虎之力终于把环境搭好了(现在都有点忘了,后面再折腾环境搭建一定要写个教程,今天先不写了). 自从环境搭好之后,就基本抛弃MDK ...
- AngularJS简单例子
双大括号标记{{}}绑定的表达式 <html ng-app> <script src="http://code.angularjs.org/angular-1.0.1.mi ...
- 一小时上手Java 8新特性
一小时上手Java 8新特性 本文摘译自 https://www.journaldev.com/2389/java-8-features-with-examples,并做了适当增补. Iterable ...
- jsp四大作用域
- ZooKeeper02
Zookeeper 分布式服务框架是 Apache Hadoop 的一个子项目,主要是用来解决分布式应用中经常遇到的一些数据管理问题.
- SpringMVC最直观的流程图
- js前后五年的时间日期万年历
<script src='bootstrap/js/jquery-1.11.2.min.js'></script>//引入JQUERY文件<div> <sel ...
- Catch That Cow (BFS广搜)
问题描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...
- Centos7 安装tomcat