当前位置:网站首页>Making unequal interval histogram with Matplotlib
Making unequal interval histogram with Matplotlib
2022-06-22 01:23:00 【JECK_ ケーキ】
I've been working on a project recently , Histogram is required , But we need different spacing ,x The axes need to be set at unequal intervals . Baidu a pile of , No solution has been found , I found one later , It's actually a bar chart , Use a bar chart , Replace with histogram . Therefore, it can not be regarded as histogram .
Record the process .
My data is like this .

There are two columns , These two columns of data should be placed in a square diagram , And it is unequal spacing . And the data are not uniform , If equispaced , It is possible to cause a lot of straight sides to be very small , Not beautiful .

therefore , Change your mind . utilize numpy Of histogram, First, make a histogram sequence with unequal spacing , Then use the bar graph , Draw the group distance and the height of the sequence .
The code is as follows :
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')
Make it out , It turns out like this .

In the process , be aware numpy Of histogram The data returned is like this .

This data , It is based on group spacing , Frequency of separate statistics .
such as pro_all This variable , stay 0, The number of this group is 0 individual , stay 1 The number of this group is 928 individual , alike
pro_valid This variable , stay 0 The number of this group is 826 individual , stay 1 The number of this group is 728 individual .
The following data are based on bins This list corresponds to . Just the number of groups , Than bins Less length 1. Because the last group is interval 100-200, in other words ,pro_all Variable , stay 100-200 The number between is 5 individual , stay 50-100 The number between is 25 individual .
therefore , Is to use the group spacing as the horizontal axis , Height as the height of the bar , Made a bar chart . And then in settings x The scale of the coordinate axis . Add a legend or something .
边栏推荐
- 【环境踩坑】No module named ‘PyQt5‘
- [environment stepping on pit] error occurs when uploading files using fastdfs test
- The appearance, space, safety and power are all upgraded. The xinjietu x70s will be put on the market from 87900 yuan
- Spark RDD案例:词频统计
- Broadening - simple strategy test
- Sparkrdd case: calculate total score
- 【Redis】ubuntu中安装redis以及redis的基本使用和配置
- Idea prompt duplicated code fragment (15 lines long)
- 4G/wifi 能耗计量插座-监测电压电流功率
- SQL statement - permission management
猜你喜欢

Pytorch learning 11:where and gather

【DailyFresh】课程记录3--商品搜索相关

ShardingSphere-proxy-5.0.0分布式哈希取模分片实现(四)

聚宽 - 简单策略试验

Simple sorting of RNN

Install easyx-vc2019
![[Environmental stepping pit] pycharm reports an error when using QT](/img/ac/665d4052919a1783ba6ac8edcb06ba.png)
[Environmental stepping pit] pycharm reports an error when using QT
![[dailyfresh] course record 3 -- product search related](/img/54/e1dbc6c2a6bd6d1f39de0f55d79976.png)
[dailyfresh] course record 3 -- product search related

Special survey of moving average strategy

Cancellation meeting and automatic refund processing of message queue
随机推荐
ShardingSphere-proxy-5.0.0分布式哈希取模分片实现(四)
[Others] analysis of got and PLT in ELF
Evc4 program cannot run on the emulator
[environment stepping on pit] error occurs when uploading files using fastdfs test
容器云是什么意思?与堡垒机有什么区别?
SQL statement - permission management
IDEA提示 ‘Optional.get()‘ without ‘isPresent()‘ check错误。
52 classes 110 common components and frameworks
. Several methods of obtaining hinstance in. Net
English grammar_ Adverb - loud /aloud / loud
English语法_副词 - loud /aloud / loudly
03 FastJson 解决循环引用
记录webscraper的使用过程
English语法_副词_fairly / rather
4275. Dijkstra sequence
Pat (a) - 1001 a+b format
FLowable运行时事务相关的表和表结构
SparkRDD 案例:计算总成绩
Integer和int区别
Promise me not to use if (obj! = null) to judge empty