My casual observations are that several locations were used for the naval ensign. The reasons may be simple or complicated due to Naval regulations, flag edict, and practical necessities.
Early pre-war images of Gato class boats in their early configurations before wartime changes such as cutting down the conning tower silhouette and many other things showed a naval ensign (national flag w/ 48 stars) on the end of the stern casing, and a commissioning pennant (a blue field only w/ 48 stars) displayed on the bow while in port. At sea a flag might have been hoisted up a line at the top of the periscope sheers, or on a staff canted back displaying the naval ensign over the end of the railing on the aft end of the 'cigarette deck'. (back end of the conning tower bridge decking). Much might have been also influenced by the Captain's wishes. Some of these early Gato photos remember might also be photos by the builders sometimes known as 'bridal shots'.
As the war progressed, operationally fleet boats spent more time on the surface to exploit their radars and high surface speeds chasing convoys, a small flag might have been run up. Speculating here, I would not even be surprised if skippers did not fly a flag at all much in their patrol areas? A small flag (or a huge one even) would not alter the intent of whether a enemy or friendly aircraft attacked first and asked later. In quick dives (then if you didn't dive quick, you just didn't dive), their wouldn't have been time to haul down 'old glory'. A small flag might have been expected to get submerged, with a formal dress flag for when in port kept below.
In port during the war, Naval reg. were followed, ensign (stern) and pennant (bow) displayed. However, informally I have seen photos of boats in port with the ensign hanging over the aft end of the conning tower cigarette deck too. If 'Uncle Charlie' Adm. Charles Lockwood SubComPac, or even Nimitz was in the vicinity, would more formal presentation of the flags might have been appropriate? A battle flag with the boat's unique ensignia and score displaying it's enemy ships the crew had sunk would be present. These 'kills' as in fighter plane parlance would have been displayed only in port with a string of little white Japanese ensigns with a orange/red dot for merchant ship targets sunk, and little 'rising sun' ensigns for Japanese warships sunk. Tied up to other boats, each boat would string out these little flags proudly displaying to each other and to history their 'scores', seemingly reflecting famous baseball averages. WWII submarine operations changed and evolved in the Pacific, developing within the Silent Service it's own special & unquie mix of formality and informality.
I don't know if I have clearified or confused, but these have been my impressions. Hope it helps.