i have data looks this:
print x
date abc def 0 2004-06-03 1.000000 1.000000 1 2004-06-04 1.000000 1.007940 2 2004-06-07 1.000000 1.023285 3 2004-06-08 1.020163 1.024712 4 2004-06-09 1.013932 1.015166 and plotting in following way:
plt.figure() x.plot().set_xticklabels(x['date']) plt.show() my issue x-axis ticks tight meaningful.
how such every n-periods shown on x-axis?
also - if know answer - how format number on x-axis date?
thanks!
edit: comments in code have been added clarification.
i'll show 2 examples answer both questions. made small csv sample data posted.
here plot showing dates:
import pandas pd import matplotlib.pyplot plt import datetime dt import numpy np import math # sample data provided in question made csv , read in using pandas data = pd.read_csv('workbook1.csv', sep=',') # 'date' column dates = data['date'] """ datetime python module work dates, time, timezones, time differences strptime(date_string, format) method used read in string (which know date) , return datetime. e.g. >>> s = '2015-05-11' >>> dt.datetime.strptime(s, '%y-%m-%d') datetime.datetime(2015, 5, 11, 0, 0) >>> print dt.datetime.strptime(s, '%y-%m-%d') 2015-05-11 00:00:00 >>> print dt.datetime.strptime(s, '%y-%m-%d').date() 2015-05-11 list comprehension: way create list create list of integers 0 9: >>> [x x in range(10)] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] create list of 1/n! n 0 99 , sum values: >>> sum( [1.0/math.factorial(n) n in xrange(100)] ) 2.7182818284590455 below use list comprehension map date strings datetimes in specified format yyyy-mm-dd """ dates = [dt.datetime.strptime(d,'%y-%m-%d').date() d in dates] now plotting:
""" want plot multiple data columns on 1 plot, use layout manager subplots(). if wanted break out individual plots each data source use subplots(n, m, o) n,m,o ints defining layout. figure window our plot reside in ax used else: plot itself, labels, ticks, ticklabels, title(s)""" fig, ax = plt.subplots() # plot 'abc' column, using red (r) line , label 'abc' used in legend ax.plot(data['abc'],'r-',label='abc') ax.plot(data['def'],'b-',label='def') # set number of ticks on x-axis list [0, ... ,r] # r number of rows in data (i.e. len(dates)) # ensures ticklabels align corresponding data point ax.set_xticks(np.arange(len(dates))) ax.set_xticklabels(dates) # set ticklabels list of datetimes ax.legend(loc=4, fontsize=10) # make legend , place in bottom-right (loc=4) plt.xticks(rotation=30) # rotate xticklabels 30 deg plt.show() 
so plot shows dates. if still cramped can change date format m/d/y , possibly skip every nth xticklabel. @ variable spacing , set needed.
# using list of datetimes above make list of strings specific # format. datetime.strftime(format) returns string in specified format short_dates = [d.strftime('%m/%d/%y') d in dates] spacing = 2 fig, ax = plt.subplots() ax.plot(data['abc'],'r-',label="abc") ax.plot(data['def'],'b-',label='def') ax.set_xticks(np.arange(len(short_dates))) ax.set_xticklabels(short_dates) ax.legend(loc=4, fontsize=10) plt.xticks(rotation=30) # each label in list of xticklabels # (we skip every n'th label in list using [::n]): # set not visible label in ax.xaxis.get_ticklabels()[::spacing]: label.set_visible(false) plt.show() 
now date format changed , skipping every other date (from given data). hope helps.
this excellent matplotlib tutorial.
Comments
Post a Comment