1

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.

Vedaant V
  • 23
  • 1
  • 3

3 Answers3

2

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.

Community
  • 1
  • 1
Zack
  • 101
  • 5
0

I would suggest the following as it is straightforward:

from collections import Counter
import matplotlib.pyplot as plt

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]
z = Counter(hours_of_sleep)

ar = 0.2
fig, ax = plt.subplots(figsize=(plt.figaspect(ar)))
for key, value in z.items():
    X = [key] * value
    Y = [item + 1 for item in range(value)]
    plt.scatter(X, Y, color='b', s=100)

plt.xlabel('response')
plt.ylabel('occurrence')
plt.title('Dotplot-histogram')
plt.tight_layout()
plt.show()

Here is the result:

enter image description here

0

You can create a dot plot by looping over the arrays of unique values and counts returned by np.unique(hours_of_sleep, return_counts=True) to draw each column of dots. Here is how you can create a plot similar to the example shown in the linked question:

import numpy as np                 # v 1.19.2
import matplotlib.pyplot as plt    # v 3.3.2

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]
values, counts = np.unique(hours_of_sleep, return_counts=True)

fig, ax = plt.subplots(figsize=(6, 3))
for value, count in zip(values, counts):
    ax.plot([value]*count, list(range(count)), c='tab:blue', marker='o',
            ms=10, linestyle='')
for spine in ['top', 'right', 'left']:
    ax.spines[spine].set_visible(False)
ax.yaxis.set_visible(False)
ax.set_ylim(-1, max(counts))
ax.set_xticks(range(int(min(values)), int(max(values)+1)))
ax.tick_params(axis='x', length=0, pad=8, labelsize=12)
ax.set_title('Hours of Sleep for Students', pad=30, fontsize=14)

plt.show()

dot_plot


You can find a more advanced version of this solution here, where some of the formatting parameters are set automatically based on the data.

Patrick FitzGerald
  • 3,280
  • 2
  • 18
  • 30