Skip to content
Advertisement

How do I convert this histogram into a dot plot/dot chart using matplotlib and 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.

Advertisement

Answer

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()

Alternative 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)

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()

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

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement