Вопрос

I have a dictionary called

Samples = {5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.51319796560976771}

Here keys of the dictionary represent the points in the x-axis, and the values of the dictionary represent the values of the point of that particular x ie y=f(x).

How do i create a histogram out of it with bin sizes=1 in the interval 0-10

Это было полезно?

Решение

You can use the keys as bin positions and the values as weights of the histogram, doing the following:

import numpy as np
import matplotlib.pyplot as plt

d = {5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.51319796560976771}
a = np.array(d.items())
a = a[np.argsort(a[:,0])]
x, weights = a.T

plt.figure(figsize=(5,4))
plt.hist(x, weights=weights)
plt.show()

which gives:

enter image description here

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top