i modified the code as follow :
def bar_plot(data, Yval, datamin=None, datamax=None):
""" doc """
x = data[0]
y = data[1]
# reduce dataset based on min max date
if datamin != datamax != None :
x = data[0][np.where((data[0]>=datamin) & (data[0]<=datamax))[0]]
y = data[1][np.where((data[0]>=datamin) & (data[0]<=datamax))[0]]
# index for labeling
xp = np.where((y<=0))[0]
xn = np.where((y>0))[0]
xx = np.where((y>0) | (y<=0))[0]
# extract positive and negative values
x_p = x[np.where(y>=0)[0]]
y_p = y[np.where(y>=0)[0]]
x_n = x[np.where(y<0)[0]]
y_n = y[np.where(y<0)[0]]
# start plotting code
fig = plt.figure(figsize=(15,5))
width = 0.7
plt.autoscale(enable=True, axis='both', tight=True)
plt.bar(xn, y_n, width, color='blue', lw=2)
plt.xticks(xx + (width/1.2), x)
plt.autoscale(enable=True, axis='both', tight=True)
plt.bar(xp, y_p, width, color='red', lw=2)
plt.title(Yval)
fig.autofmt_xdate()
plt.xlabel("Year")
plt.ylabel(Yval)
plt.axhline(0, color='black', lw=2)
plt.grid(True)
Il giorno Mar 31, 2012, alle ore 10:21 PM, Massimo Di Stefano ha scritto:
>
> Hi All
>
> i'm bring to do simple bar plot formatting the xlabe as 'date' using a
> syntax like : fig.autofmt_xdate(bottom=0.1)
> but something is wrong in my code, please have you any hints on ghow to
> proper display the dates (Year) along the x axis
> and how to leave a margin on the left and on the right of the plot? (my
> example has a margin only on the right, while the first bar is adjacent to
> the Y axis)
>
>
> ###
>
> import numpy as np
> import matplotlib.pyplot as plt
>
> x = np.array([1969,1970,1971,1972,1973,1974])
> y = np.array([-3,10,23,-4,-5,6])
> data = [x,y]
>
> print x
> print y
>
>
>
>
> def bar_plot(data, Yval, datamin=None, datamax=None):
> """ doc """
> fig = plt.figure()
> fig.autofmt_xdate(bottom=0.1)
> ax1 = fig.add_subplot(211)
> x = data[0]
> y = data[1]
> if datamin != datamax != None :
> x = data[0][np.where((data[0]>=datamin) & (data[0]<=datamax))[0]]
> y = data[1][np.where((data[0]>=datamin) & (data[0]<=datamax))[0]]
> x_p = x[np.where(y>=0)[0]]
> y_p = y[np.where(y>=0)[0]]
> x_n = x[np.where(y<0)[0]]
> y_n = y[np.where(y<0)[0]]
> ax1.bar(x_n, y_n, facecolor='b')
> ax1.bar(x_p, y_p, facecolor='r')
> ax1.grid(True)
> ax1.set_xlabel('Year')
> ax1.set_ylabel(Yval)
> ax1.set_title(Yval)
> ax1.axhline(0, color='black', lw=1)
> plt.show()
>
> bar_plot(data, 'Var', datamin=1970, datamax=1973)
>
> ###
------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Matplotlib-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users