When does $\frac{\partial f}{\partial y} =0$ imply $f(x,y)=g(x)$? Let $U\subseteq\mathbb R^2$ be an open subset and $f:U\to\mathbb R$ a continuously differentiable function such that $$\forall (x,y)\in U:\frac{\partial f}{\partial y} =0.$$
Which conditions must $U$ and $f$ fulfill for $f$ to be a function in $x$ only, i. e. $\exists g:\mathbb R\to\mathbb R\forall (x,y)\in U:f(x,y)=g(x)$?
I was thinking about $U$ having to be connected, because elsewise we could have something like $U=\left\lbrace (x,y)\in\mathbb R^2\mid y<1\vee y>2\right\rbrace$ and define $f$ as $$f(x,y)=\begin{cases}x,&y<1\\2x,&y>2\end{cases}$$ which would make the partial derivative with respect to $y$ equal to zero without $f$ being independent of $y$.
However, is $U$ being a connected space a sufficient condition? How could I prove that or what would a counterexample look like?

Here I make a proof of the theorem with the hypothesis of convexity.
If you use the "directional mean value theorem", you obtain that $\forall \ z_1=(x,y), \ z_2=(x,y') \in U,$ the line segment $\gamma$ between $z_1$ and $z_2$ is totally contained in $U.$ So, you have that $$f(x,y)-f(x,y')=(y'-y) \ \partial_yf(x,c_{y,y'})=0,$$ where $c_{y,y'} \in (y,y'),$ which means that $f(x,y)=f(x,y')=g(x).$
