当前位置:网站首页>matplotlib 制作不等间距直方图
matplotlib 制作不等间距直方图
2022-06-22 00:27:00 【JECK_ケーキ】
最近在做一个项目,需要用到直方图,不过需要不等间距的,x轴需要设置成不等的间距。百度了一堆,也没找到解决方法,后来找到一个,实际上是做得条形图,用条形图,替换成直方图。因此也不能算是直方图。
记录一下过程。
我的数据是一个这样的。

有两列,这两列数据要放在一个直方图里,并且是不等间距。而且数据都不是均匀的,如果等间距,就有可能造成很多直方很小,不美观。

所以,换一下思路。利用numpy的histogram, 先制作一个不等间距的直方图数列,然后用条形图,把组距和数列的高度划出来。
代码如下:
import re
import pandas as pd
import os
from collections import Counter
import time
import requests
from scrapy import Selector
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=15)
bins = [0, 1, 2, 3, 4, 5, 10, 20, 50, 100, 200]
height = [ np.histogram(xs, bins=bins)[0] for xs in [pro_all, pro_valid]]
left, n = np.arange(len(bins)-1), len(height)
fig, ax = plt.subplots()
for j, h in enumerate(height):
ax.bar(left + j/n, h, width = 1.0/n, label = leg[j])
ax.set_xticks(np.arange(0, len(bins)))
ax.set_xticklabels(map(str, bins))
plt.legend(loc = 'upper right')
做出来,结果是这样的。

这个过程中,注意到numpy的histogram 返回的数据是这样的。

这个数据,就是按照组距,分别统计的频数。
比如 pro_all 这个变量,在0,这一组的个数是0个, 在1这一组的个数是928个, 同样的
pro_valid这个变量,在0这一组的个数是826个, 在1这一组个数是728个。
后面的数据都是按照bins这个列表对应的。只不过分组的数量,比bins的长度少1。因为最后一组是区间100-200, 也就是说,pro_all变量,在100-200之间的个数是5个,在50-100之间的个数是25个。
所以,就是用组距作为横轴,高度作为条形的高度,做了一个条形图。然后在设置x坐标轴的刻度。添加图例什么的。
边栏推荐
- HDOJ - Is It A Tree?
- 4275. Dijkstra序列
- crf*.bdb文件过大问题处理
- [redis] install redis in Ubuntu and the basic usage and configuration of redis
- cygwin下面用mysql client连接服务器的问题
- ==And equals
- Idea prompt 'optional Get() 'without' ispresent() 'check error.
- 03 fastjson resolving circular references
- Sum of four numbers [array sort + double pointer]
- 4g/wifi energy consumption metering socket - monitoring voltage, current and power
猜你喜欢

编译原理-递归下降子程序法

Yolov3 3D semantic point cloud paper reading

0x00007ffff3d3ecd0 in _ IO_ vfprintf_ internal (s=0x7ffff40b5620 <_IO_2_1_stdout_>

Pytorch learning 01: gradient descent for simple linear regression

动态规划-01背包,分割等和子集,最后一块石头的重量

Go Technology Daily (June 20, 2022) -- go: simple optimization notes

4g/wifi energy consumption metering socket - monitoring voltage, current and power
![Sum of four numbers [array sort + double pointer]](/img/9e/e1932120a9b69847898e89c45e2eb4.png)
Sum of four numbers [array sort + double pointer]

从简单实例来看 left join 如何去重

利用SSM框架实现用户登陆
随机推荐
Pytorch learning 01: gradient descent for simple linear regression
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
mysql整理
SQL statement - data update, modification and deletion
4g/wifi energy consumption metering socket - monitoring voltage, current and power
English语法_副词_fairly / rather
BigDecimal基本使用
手写数据库连接池
2. add two numbers
【环境踩坑】在自己电脑上搭建FastDFS
cygwin下面用mysql client连接服务器的问题
Error 4 opening dom ASM/Self in 0x8283c00
判断String类型是否为空,判断list集合是否为空
Xshell比SecureCRT差吗?
HDOJ - Is It A Tree?
Div set scrolling and monitor scrolling distance
【DailyFresh】课程记录2
Pytorch learning 09: basic matrix operations
3分钟,带你玩转聊天机器人自动化【顶级模板】
Pytorch learning 06: tensor dimension transformation