1
import matplotlib.pyplot as plt
import matplotlib
import numpy as np 
import time
d ={'5000cca234c1c445': {382877: 7, 382919: 3},
'5000cca234c94a2e': {382873: 1, 382886: 1},
'5000cca234c89421': {383173: 1, 383183: 2, 382917: 1, 382911: 1},
'5000cca234c5d43a': {382889: 1, 382915: 1, 382917: 8},
'5000cca234c56488': {382909: 2, 382911: 5}}

colors = list("rgbcmyk")

for data_dict in d.values():
    x = data_dict.keys()
    y = data_dict.values()
    plt.scatter(x,y,color=colors.pop(),s = 60)
    plt.ylabel("Errors" , fontsize=18, color="Green")
    plt.xlabel("Occured at",fontsize=18, color="Green")
    plt.title("DDN44a" , fontsize=25, color="Blue")
    plt.gca().get_xaxis().get_major_formatter().set_useOffset(False)
    plt.xticks(rotation='vertical') 
    #plt.ylim(min(y),max(y))
plt.grid()

plt.legend(d.keys())
plt.show()

the x-axis values( 382873,382866,382873.......) are converted dates using mktime() function by following line of code.

bin = int(mktime(dt) / 3600)

(I am trying to see how many time does the error occur in one hour) On the graph, x-axis values are shown as 382873,382866,382873..etc. I want them to convert them back to the dates(with MM/DD/YY MM:HH:SS FORMAT) they represent. Also I want the date to show directly below the scatter plot (dot) with same color as the dot.

Also, I am not sure where make the changes in the code to solve the problem. Feel free to edit my code . Thank you.

CT Zhu
  • 52,648
  • 17
  • 120
  • 133
user3262210
  • 117
  • 1
  • 3
  • 7

1 Answers1

1

Just add this:

plt.xticks(plt.xticks()[0], [time.strftime("%m/%d/%y, %H:%M:%S", time.localtime(item)) for item in plt.xticks()[0]*3600])

enter image description here

To have the date underneath the dots, two more extra lines:

for x, y in dict(itertools.chain(*[item.items() for item in d.values()])).items():
    plt.text(x, y, time.strftime("%m/%d/%y, %H:%M:%S", time.localtime(x)), ha='center', va='top')

but it will look very busy:

enter image description here

CT Zhu
  • 52,648
  • 17
  • 120
  • 133
  • how would I show more values(dates) on the x-axis...so for example, I want to show more dates that come between 09/03/13 - 09/06/13, what would I need to do? THank you. – user3262210 Mar 07 '14 at 22:23
  • @user3262210, see this post: http://stackoverflow.com/questions/12608788/changing-the-tick-frequency-on-x-or-y-axis-in-matplotlib – CT Zhu Mar 07 '14 at 22:26