怎么用python画直方图中怎么设置数据范围,如何在Python中使用Matplotlib绘制带有数据列表的直方图?...

I am trying to plot a histogram using the matplotlib.hist() function but I am not sure how to do it.

I have a list

probability = [0.3602150537634409, 0.42028985507246375,

0.373117033603708, 0.36813186813186816, 0.32517482517482516,

0.4175257731958763, 0.41025641025641024, 0.39408866995073893,

0.4143222506393862, 0.34, 0.391025641025641, 0.3130841121495327,

0.35398230088495575]

and a list of names(strings).

How do I make the probability as my y-value of each bar and names as x-values?

解决方案

If you want a histogram, you don't need to attach any 'names' to x-values, as on x-axis you would have data bins:

import matplotlib.pyplot as plt

import numpy as np

%matplotlib inline

np.random.seed(42)

x = np.random.normal(size=1000)

plt.hist(x, density=True, bins=30) # `density=False` would make counts

plt.ylabel('Probability')

plt.xlabel('Data');

mP5pc.png

You can make your histogram a bit fancier with PDF line, titles, and legend:

import scipy.stats as st

plt.hist(x, density=True, bins=30, label="Data")

mn, mx = plt.xlim()

plt.xlim(mn, mx)

kde_xs = np.linspace(mn, mx, 301)

kde = st.gaussian_kde(x)

plt.plot(kde_xs, kde.pdf(kde_xs), label="PDF")

plt.legend(loc="upper left")

plt.ylabel('Probability')

plt.xlabel('Data')

plt.title("Histogram");

VI6OD.png

However, if you have limited number of data points, like in OP, a bar plot would make more sense to represent your data (then you may attach labels to x-axis):

x = np.arange(3)

plt.bar(x, height=[1,2,3])

plt.xticks(x, ['a','b','c'])

a0Mvt.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值