Why can a partial derivative be added to a hamiltonian in canonical transformations? In canonical transformations, how come we allow hamiltonian to change by a partial derivative of time?
$$H'(P, Q, t) = H(p, q, t) + \frac{\partial F}{\partial t}.$$
Here $F$ is the generating function.
I mean geometrically that is not how a function should be transformed when there is a change of variables. Geometrically it should be $$H'(P, Q, t) = H(p, q, t).$$ 
In Lagrangian mechanics it is indeed so
$$L'(Q, \dot{Q}, T) = L(q, \dot{q}, t).$$
 A: In a canonical transformation, the new hamiltonian could have nothing to do with the initial hamiltonian, it just have to preserve Hamilton's equations. So in the new variables $(Q,P,t)$ you have to have that 
$$\dot{Q} = \frac{\partial K}{\partial P} \qquad \dot{P} = -\frac{\partial K}{\partial Q}$$
where $K$ is the new Hamiltonian. Whenever this happens 
$$K(Q,P,t) = H(q(Q,P),p(Q,P),t)$$ 
we call the transformation completely canonical (with the added bonus that the transformation is time independent), and it's a particular type of canonical transformation.
A: A more geometric approach is to consider the $(2n+1)$-dimensional contact manifold ${\cal M}$ with coordinates $(q^i,p_j,t)$. The Hamiltonian action functional is 
$$S_H[\gamma]~=~\int_I \gamma^{\ast} \Theta, \qquad \Theta~=~p_j \mathrm{d}q^j -H \mathrm{d}t,  \tag{1}$$
where $\gamma:I\to {\cal M}$ is a curve. This action formulation (1) is world-line (WL) reparametrization invariant. Let us for simplicity work in the static gauge $\gamma^0(t)=t$. The Euler-Lagrange (EL) equations (i.e Hamilton's equations) remain the same if we change the contact 1-form $\Theta$ by an exact 1-form
$$ P_j \mathrm{d}Q^j -K\mathrm{d}t ~=~ \Theta^{\prime}~=~\Theta- \mathrm{d}F.\tag{2}$$
From this geometric perspective, the transform law
$$ K~=~H + \frac{\partial F}{\partial t} \tag{3}$$
is just the standard way how the $t$-component $\Theta_t=-H$ of the contact 1-form $\Theta$ transforms under a change by an exact 1-form (given various other restrictions on the transformation).
References:


*

*S. G. Rajeev, A Hamilton-Jacobi Formalism for Thermodynamics, Annals. Phys. 323 (2008) 2265, arXiv:0711.4319.

*H. Geiges, An introduction to contact topology, 2008. (A pdf file of lecture notes from 2004 can be found on the author's webpage.)
