如何使用matplotlib和numpy将此直方图转换为点状图/点状图? [英] How do I convert this histogram into a dot plot/dot chart using matplotlib and numpy?

查看:81
本文介绍了如何使用matplotlib和numpy将此直方图转换为点状图/点状图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据学生的睡眠时间创建一个点状图/点状图,但我能获得的最接近的是与我的数据匹配的直方图.由于纯粹的经验不足或与数据不兼容,下面尝试提供的方法对我不起作用.任何帮助将不胜感激.

I'm trying to create a dot plot/dot chart based on students' hours of sleep, but the closest I was able to get was a histogram which matched my data. The method I tried which will be provided below didn't work for me either due to my sheer inexperience or incompatibility with my data. Any help would be greatly appreciated.

我已经尝试过类似的答案,它是:

I've already tried a similar answer which was this: How to create a "dot plot" in Matplotlib? (not a scatter plot)

此方法将睡眠时间中的浮点值四舍五入,这使绘图变得不正确,或者也许我只是错误地使用了它.我希望使用我的确切示例来解决方案,因为我对编程还是很陌生的,可能对其他方面一无所知.

This method rounded the float values in hours of sleep up, which was making the plot incorrect, or perhaps I was just using it wrong. I would appreciate a solution using my exact example, because I'm still pretty new to programming and likely won't understand much else.

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

hours_of_sleep = [9, 6 ,8, 6, 8, 8, 6, 6.5, 6, 7, 9, 4, 3, 4, 5, 6, 11, 6, 3, 6, 6, 10, 7, 8, 4.5, 9, 7, 7]
bin_list = []

for number in hours_of_sleep:
    if number not in bin_list:
        bin_list.append(number)
        bin_list.sort()
        item_1 = bin_list[0]
        item_2 = bin_list[-1]


proper_bin = np.arange(item_1, item_2+1, 0.5)


plt.hist([hours_of_sleep], bins=proper_bin, rwidth= 0.8)
plt.title('Hours of Sleep for Students')

plt.show()

最后,我想提供与用户在我已提供的链接中提出问题的用户提供的点图示例类似的内容.

I want to end up with something similar to the dot plot example provided by the user who asked the question in the link I've already provided.

推荐答案

我认为这可以回答您的问题:

I feel that this answers your question: How to create a "dot plot" in Matplotlib? (not a scatter plot)

我使用的方法大致相同.

I'm using more or less the same method.

import matplotlib.pyplot as plt
import numpy as np

hours_of_sleep = [9, 6 ,8, 6, 8, 8, 6, 6.5, 6, 7, 9, 4, 3, 4, 5, 6, 11, 6, 3, 6, 6, 10, 7, 8, 4.5, 9, 7, 7]
bins = np.arange(0, max(hours_of_sleep) + 1, 0.5)

hist, edges = np.histogram(hours_of_sleep, bins=bins)

y = np.arange(1, hist.max() + 1)
x = np.arange(0, max(hours_of_sleep) + 0.5, 0.5)
X,Y = np.meshgrid(x,y)

plt.scatter(X, Y, c = Y<=hist, cmap="Blues")
plt.xticks(np.arange(max(hours_of_sleep) + 2))
plt.yticks([])
plt.title('Hours of Sleep for Students')
plt.show()


替代方法

import matplotlib.pyplot as plt
import numpy as np

hours_of_sleep = [9, 6 ,8, 6, 8, 8, 6, 6.5, 6, 7, 9, 4, 3, 4, 5, 6, 11, 6, 3, 6, 6, 10, 7, 8, 4.5, 9, 7, 7]
bins = np.arange(0, max(hours_of_sleep) + 1, 0.5)

hist, edges = np.histogram(hours_of_sleep, bins=bins)

y = np.arange(1, hist.max() + 1)
x = np.arange(0, max(hours_of_sleep) + 0.5, 0.5)
X,Y = np.meshgrid(x,y)

Y = Y.astype(np.float)
Y[Y>hist] = None
plt.scatter(X, Y)
plt.xticks(np.arange(max(hours_of_sleep) + 2))
plt.yticks([])
plt.title('Hours of Sleep for Students')
plt.show()

希望这会有所帮助.:)
阅读一些
Matplotlib文档也会对您有所帮助.

Hope this helps. :)
Reading some Matplotlib Documentations will help you too.

这篇关于如何使用matplotlib和numpy将此直方图转换为点状图/点状图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆