Wednesday, April 27, 2016

Plot histogram on a clock

Today I want to plot a histogram on a 24 hour clock (the data is aggregated from real life data that bined on each hour of the day). Here's the code, and you can download the code from Qingkai's Github.
In [1]:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_context('poster')
sns.set_style('white')
%pylab inline
Populating the interactive namespace from numpy and matplotlib

(1) First generate random data from 0 - 23

In [2]:
arr = np.random.randint(0, 24, size = 500)

(2) Plot on the clock

In [3]:
N = 23
bottom = 2

# create theta for 24 hours
theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)

# make the histogram that bined on 24 hour
radii, tick = np.histogram(arr, bins = 23)

# width of each bin on the plot
width = (2*np.pi) / N

# make a polar plot
plt.figure(figsize = (12, 8))
ax = plt.subplot(111, polar=True)
bars = ax.bar(theta, radii, width=width, bottom=bottom)

# set the lable go clockwise and start from the top
ax.set_theta_zero_location("N")
# clockwise
ax.set_theta_direction(-1)

# set the label
ticks = ['0:00', '3:00', '6:00', '9:00', '12:00', '15:00', '18:00', '21:00']
ax.set_xticklabels(ticks)

plt.show()
In [ ]:
 

10 comments:

  1. I have read your blog its very attractive and impressive. I like it your blog...R Programming Training in Bangalore

    ReplyDelete
  2. People tired to find a relevant place where they can know real facts and myths about the topic mentioned by author of this blog. I want to help a lot of needy people through this blog to come up at my blog to know the real facts and myths regarding this topic. นาฬิกา ถูก ๆ

    ReplyDelete
  3. Such a informative post.Thanks for sharing your knowledge with us.keep it up for updating post..
    iot training in chennai | top 10 embedded training institutes in chennai .

    ReplyDelete

  4. Thanks for sharing the descriptive information on Python tutorial. It’s really helpful to me since I'm taking Python training. Keep doing the good work and if you are interested to know more on Python, do check this Python tutorial.:-https://www.youtube.com/watch?v=HcsvDObzW2U

    ReplyDelete
  5. Appreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of.
    fire and safety course in chennai

    ReplyDelete
  6. Very helpful and attractive, many thanks. Small correction I think- N = 24? You can see the segments are very slightly too big.

    ReplyDelete
  7. Five weeks ago my boyfriend broke up with me. It all started when i went to summer camp i was trying to contact him but it was not going through. So when I came back from camp I saw him with a young lady kissing in his bed room, I was frustrated and it gave me a sleepless night. I thought he will come back to apologies but he didn't come for almost three week i was really hurt but i thank Dr.Azuka for all he did i met Dr.Azuka during my search at the internet i decided to contact him on his email dr.azukasolutionhome@gmail.com he brought my boyfriend back to me just within 48 hours i am really happy. What’s app contact : +44 7520 636249‬

    ReplyDelete