python - Formatting X axis with dates format Matplotlib -


I have written a code that gives a plot to the user for the stock market for the last seven days over the last seven days is. The problem I have is that I want to format X axis in a YYMMDD format. I do not even understand what the meaning of 2.014041 A7 at the end of the X axis is.

Values ​​for X are:

20140421.0, 20140417.0, 2014041.0, 20140415.0, 2014.04.0.0, 20140411.0, 20140410.0 < / P>

There are values ​​for y:

531.17, 524.94, 519.01, 517.96, 521.68, 519.61, 523.48

My The code is:

  mini = min (y) maxi = maximum (y) min = mini-75 max = maxi + 75 min2 = int (min (x)) maxi2 = int (max. (X)) plt.close ('all') fig, ax = plt.subplots (1) pylab.ylim ([minimum, max]) pylab.xlim ([mini2, maxi2]] ax.plot (x, y) ax.plot (x, y, 'ro') ax.plot (x, m * x + c) ax.grid () ax.plot ()   

When you use your method If you plot your data using, then you are just plotting your y data such as against x numbers in x 20140421.0 (which I think you want to mean by date 21/04/2014).

You need to change your data from these flows to understand TS matplotlib In the appropriate format, the code below takes your two lists (X, Y) and converts them. Not. Import the NMP as import matplotlib.pyplot plt import matplotlib.dates as mdates import datetime as dt # original data raw_x = [20140421.0, 20140417.0, 20140421.0, 20140417.0, 20140416.0, 20140415.0, 20140414.0 , 20140411.0, 20140410.0] y = [531.17, 524.94, 51 9 .01, 517.96, 521.68, 51 9.6, 523.48] # Convert your X-data to a suitable format. # Date_fmt is the correct format string for your data. In this case # we are using 'YYYYMMDD.0' because your dates are actually float Date_fmt = '% Y% M% d 0' # on your date Use the list's understanding to convert objects to time. # The comp striptime is used to convert from string to a datetime # object Dt_x = [dt.datetime.strptime (str (i), date_fmt) in raw_x for i] # at the end, we have the datetime object To matplotlib.dates.date2num x = [mdates.date2num x = I] dt_x in the format used by matplotlib # for i] # Now to actually plot your data, fig = plt.subplots () When dealing with time data, use plot_date instead of plot. Ax.plot_date (x, y, 'bo-') # Create a date format object that will formatted your tick label correctly. # In my question, I have selected "YYMMDD" date_formatter = mdates.DateFormatter ('% y% m% d') # Set the main tick format to use your date format. Ax.xaxis.set_major_formatter (date_formatter) # These X-axis tick labels rotate slightly, so they fit well. Anji.autofmt_xdit () plt.show ()

This code is commented on, so that it is easily self explanatory. Details on various modules can be found below:

  1.  example

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -