python plot hist(graph) with percentile

放肆的年华 提交于 2019-12-13 04:15:15

问题


I have a couple of questions, and i tried but i couldn't solve it. Let me help.

This is the question. There is an unknown histogram, and I want to guess the histogram(Not exact histogram is okay).

I got some information about histogram. Given Information: min, max, size, mean, percentile(25%, 50%, 75%)

  1. I want to know how i can get the graph satisfying those conditions.
  2. Why this code doesn't work??

Thank you.

-----------This is what i tried-------------

import pandas as pd
import numpy as np
import random
import matplotlib.pyplot as plt


class R_distribution():
    def __init__(self, min, max, size, mean, Q1, Q2, Q3):
        self.min = min
        self.max = max
        self.size = size
        self.error = 10
        self.mean = mean
        self.median = Q2
        self.Q1 = Q1
        self.Q3 = Q3
        # df = pd.DataFrame(np.random.uniform(self.min, self.max, size=self.size))
        df = pd.DataFrame(np.random.randn(size)*(max-1) + min)
        _ = df.describe()
        standard = {'mean': mean, '50%': Q2, '25%': Q1, '75%': Q3}
        loop_switch = []
        i = 0
        while len(set(loop_switch)) < 4:
            for s in standard.keys():
                if float(_.loc[s]) >= standard[s] - self.error and float(_.loc[s]) <= standard[s] + self.error:
                    loop_switch.append(s)
                else:
                    # df = pd.DataFrame(np.random.uniform(self.min, self.max, size=self.size))
                    df = pd.DataFrame(np.random.randn(size) * (max - 1) + min)
            i += 1
            if i%1000 == 0:
                print(str(i)+"th loop")
                print(set(loop_switch))
        print(df)

if __name__ == '__main__':
    a = R_distribution(min=1.0, max=70.0, size=15, mean=13.53, Q1=2.00, Q2=3.50, Q3=10.00)

来源:https://stackoverflow.com/questions/53827292/python-plot-histgraph-with-percentile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!