Histogram with independent line matplotlib
Question: Working with matplotlib (1.3.1-2), python 2.7.

I create a a stacked histogram with timely distribution on the x-Axis the
following:

    
    
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    
    
    #the dates for plotting (numpy array)
    date1 = [735133.84893519  734066.13166667  732502.86928241  732502.81313657 732502.81313657  735133.85021991  735133.85019676  733935.8158912 733935.81766204  733361.04634259  733361.04921296  733361.05106481 733935.81671296  734010.75708333  734772.85976852  734010.75684028]
    date2 = [732582.51802083  732582.51796296  734893.73981481  735629.50372685 735629.50369213  732874.66700231  734663.6618287   734687.42241898 734687.4216088   734687.42064815  733616.43398148  734663.67599537 734600.71085648  734598.31212963  734598.31207176  734600.71082176 734598.31199074  735044.42799769  734643.24407407  734617.59635417]
    date3 = [734372.11476852  734372.11424769  734359.19949074  734359.19871528 734359.19790509  734359.19711806  734359.19630787  734359.19534722 734359.19452546  734359.19372685  734359.1921412   734359.14888889 734359.14819444  734359.1475      734359.14677083  734359.14599537]
    
    
    #plot it
    fig, ax = plt.subplots(1,1)
    ax.hist([date2, date2, date3], bins = 200, stacked = True, normed = True, edgecolor = 'None', linewidth = 0, color = ("#007d13", "#2eb1f3", "#aaa1ff"))
    plt.legend(["date1", "date2", "date3"])
    ax.autoscale(enable = True, axis = "x", tight = True)
    ax.xaxis.set_major_locator(mdates.YearLocator())
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%y'))
    plt.tick_params(axis = "both", which = "both", direction = 'out' )
    plt.xticks(rotation = 50)
    plt.grid()
    plt.show()
    

What I need now is a line in there. That line will be defined by a date and a
value.

    
    
    #numpy array
    point1 = [734598.31212963 66352]
    point2 = [732582.51802083 551422]
    point3 = [735133.84893519 77162]
    

As you can see, the value of these dates will be way higher than the
cumulative ones from my dates. Thus, I will need second different scaled
y-Axis as well.

Any suggestions?


Answer: you can perfectly have 2 different scale in matplotlib. See documentation
here:

<http://matplotlib.org/examples/api/two_scales.html>

complete code here :

    
    
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    
    
    #the dates for plotting (numpy array)
    date1 = [735133.84893519,734066.13166667,732502.86928241,732502.81313657,732502.81313657,735133.85021991,735133.85019676,733935.8158912,733935.81766204,733361.04634259,733361.04921296,733361.05106481,733935.81671296,734010.75708333,734772.85976852,734010.75684028]
    
    date2 = [732582.51802083,732582.51796296,734893.73981481,735629.50372685,735629.50369213,732874.66700231,734663.6618287, 734687.42241898,734687.4216088, 734687.42064815,733616.43398148,734663.67599537,734600.71085648,734598.31212963,734598.31207176,734600.71082176,734598.31199074,735044.42799769,734643.24407407,734617.59635417]
    
    date3 = [734372.11476852,734372.11424769,734359.19949074,734359.19871528,734359.19790509,734359.19711806,734359.19630787,734359.19534722,734359.19452546,734359.19372685,734359.1921412, 734359.14888889,734359.14819444,734359.1475,734359.14677083,734359.14599537]
    
    
    #plot it
    fig, ax = plt.subplots(1,1)
    ax.hist([date2, date2, date3], bins = 200, stacked = True, normed = True, edgecolor = 'None', linewidth = 0, color = ("#007d13", "#2eb1f3", "#aaa1ff"))
    plt.legend(["date1", "date2", "date3"])
    ax.autoscale(enable = True, axis = "x", tight = True)
    ax.xaxis.set_major_locator(mdates.YearLocator())
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%y'))
    plt.tick_params(axis = "both", which = "both", direction = 'out' )
    plt.xticks(rotation = 50)
    plt.grid()
    
    
    # setting your values in a correct form
    point1 = [734598.31212963,66352]
    point2 = [732582.51802083,551422]
    point3 = [735133.84893519,77162]
    t=[point2[0],point1[0],point3[0]]
    val=[point2[1],point1[1],point3[1]]
    
    
    ax2 = ax.twinx() 
    
    ax2.plot(t, val, 'r')
    ax2.set_ylabel('axe2', color='r')
    for tl in ax2.get_yticklabels():
        tl.set_color('r')
    plt.show()
    

