Matplotlib X-axis dates
Joaquin Alzola
Joaquin.Alzola at lebara.com
Thu Jun 2 11:45:22 EDT 2016
Hi Guys
Have been all day looking for a solution on the x-axis and the time plot.
The output that I get is this with all the dates shift to the right overlapping each other. http://postimg.org/image/fs4tx83or/
I want the x-axis to start at 0 and to finish after the 3 entries on the DB.
X Axis should show (and it does but shift one over each other to the right):
2016-06-02 11:15:00
2016-06-02 11:20:00
2016-06-02 11:25:00
Data on the Cassandra DB:
cqlsh:lebara_diameter_codes> select * from nl_lebara_diameter_codes;
country | service | date | errorcode2001 | errorcode3056 | errorcode4010 | errorcode4012 | errorcode4998 | errorcode4999 | errorcode5012
---------+---------+---------------------------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------
NL | gprs | 2016-06-02 11:15:00.000000+0000 | 3061 | 0 | 1 | 0 | 0 | 50 | 741
NL | gprs | 2016-06-02 11:20:00.000000+0000 | 3204 | 0 | 9 | 0 | 0 | 56 | 725
NL | gprs | 2016-06-02 11:25:00.000000+0000 | 3044 | 0 | 8 | 0 | 0 | 64 | 722
Code:
sql = country_sql("NL",ago.strftime('%Y-%m-%d %H:%M:%S'),"gprs")
result = sql.sql_statement_range()
for i in result:
country = i.country
service = i.service
x_list.append(i.date)
code_2001.append(i.errorcode2001)
code_4012.append(i.errorcode4012)
code_4998.append(i.errorcode4998)
code_4999.append(i.errorcode4999)
code_5012.append(i.errorcode5012)
code_3056.append(i.errorcode3056)
code_4010.append(i.errorcode4010)
print(x_list) --> Output is [datetime.datetime(2016, 6, 2, 11, 15), datetime.datetime(2016, 6, 2, 11, 20), datetime.datetime(2016, 6, 2, 11, 25)]
#date_nump=date2num([i for i in x_list])
date_nump=date2num(x_list)
print(date_nump) --> output is [ 736117.46875 736117.47222222 736117.47569444]
fig = plt.figure(figsize=(12,6))
ax = fig.add_subplot(111)
ax.set_title(country.upper() + " " + service,fontsize=16, fontweight='bold')
plt.plot(np.array(code_2001),label="2001")
plt.plot(np.array(code_3056),label="3056")
plt.plot(np.array(code_4010),label="4010")
plt.plot(np.array(code_4012),label="4012")
plt.plot(np.array(code_4998),label="4998")
plt.plot(np.array(code_4999),label="4999")
plt.plot(np.array(code_5012),label="5012")
lgd = plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
ax.set_xscale('linear')
ax.set_xticks(date_nump)
ax.set_xticklabels(num2date(date_nump))
#ax.xaxis.set_major_locator(HourLocator(byhour=range(0,24,2)))
ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d %H:%M:%S'))
#ax.xaxis.set_minor_locator(MinuteLocator())
ax.grid(True,linestyle='-',color='0.75')
plt.gcf().autofmt_xdate(bottom=0.2, rotation=30, ha='right')
plt.ylabel('Total Amount',fontsize=12, fontweight='bold')
plt.xlabel('Date/Time',fontsize=12, fontweight='bold')
plt.gca().set_ylim(bottom=0)
plt.savefig('testplot.png',bbox_extra_artists=(lgd,), bbox_inches='tight')
plt.show()
This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt.
More information about the Python-list
mailing list