Exercises — Publication-quality figures — LaTeX labels, colormaps, DPI
5.4.19 · D4· Coding › Scientific Computing (Python) › Publication-quality figures — LaTeX labels, colormaps, DPI
Har problem mein teen knobs chalti hain. Yeh picture dhyaan mein rakho:

Left dial (Dots) sharpness control karta hai. Middle dial (Letters) control karta hai ki tumhara text paper se match kare ya nahi. Right dial (Colors) control karta hai ki tumhara data honestly dikhaya ja raha hai ya nahi. Yahi D-L-C mnemonic hai jo parent note se aata hai.
Level 1 — Recognition
Yeh test karta hai ki tum sahi tool naam le sako. Koi arithmetic nahi.
Problem 1.1
Ek colormap jisme data value mein equal steps se brightness ki perception mein bhi equal steps milein — usse kya kehte hain? Ek example do jo yeh satisfy kare.
Recall Solution 1.1
Kya poocha gaya hai: "honest brightness" property ka naam.
Answer: ek perceptually uniform colormap. Example: viridis (aur bhi hain: cividis, magma, plasma).
Kyun: "perceptually uniform" ka matlab hi hai — perception (jo aankh dekhti hai) uniformly (equal steps mein) badlati hai jab data equal steps mein badalta hai — koi bright band jump karke fake edge nahi banata.
Problem 1.2
Kaunsi single figure setting figure ki physical printed size fix karti hai — dpi ya figsize?
Recall Solution 1.2
Answer: figsize (inches mein measure hoti hai).
Kyun: figsize=(w,h) hi inch canvas hai. dpi sirf yeh batata hai ki un inches mein kitne dots pakoge — yeh sharpness badalta hai, physical width kabhi nahi. dpi kitna bhi badhao, 3.5-in figure 3.5 in wide hi rahega; bas crisp ho jayega.
Problem 1.3
Tumhare paas data hai jo se tak jaata hai, jahan ka matlab hai "koi anomaly nahi". Sequential ya diverging colormap?
Recall Solution 1.3
Answer: diverging (jaise coolwarm, RdBu).
Kyun: data ka ek meaningful center hai (). Diverging map center ko neutral color deta hai aur dono directions ko do opposite hues mein push karta hai, taaki reader instantly dekh sake "zero ke kis side mein hoon".
Level 2 — Application
Ab parent note ka pixel formula use karo: Yahan figure ki width/height inches mein hain aur DPI (dots per inch) hai. Inches DPI ke "per inch" ke against cancel ho jaate hain (yeh Dimensional analysis hai), aur sirf dots bachte hain.
Problem 2.1
Ek figure figsize=(4.0, 3.0) declare ki gayi hai aur dpi=300 par save ki gayi. Pixel resolution kya hogi?
Recall Solution 2.1
Kya karna hai: har inch dimension ko DPI se multiply karo. Kyun kaam karta hai: ; inch unit cancel ho jaata hai.
Problem 2.2
Ek journal double-column width in aur fine line art ke liye 600 dpi maangta hai. Kya pixel width milegi, aur figsize=(7.0, 4.2) ke liye height kitne pixels mein hogi?
Recall Solution 2.2
To px. 600 kyun, 300 nahi? "Fine line art" (patli lines, chhota text) mein zyada dots per inch chahiye taaki patli strokes pixels ke beech mein kho na jayein.
Problem 2.3
Tumhara figsize=(3.5, 2.6) hai aur tumhe kam se kam px width chahiye. Minimum DPI kya hoga?
Recall Solution 2.3
rearrange karo. Kyun rearrange kiya: wohi formula, unknown ke liye solve kiya. kuch bhi requirement meet karta hai.
Level 3 — Analysis
Yahan tumhe mechanism explain karna hai, sirf compute nahi karna.
Problem 3.1
ax.set_xlabel(r"$\lambda$ (nm)") mein leading r kyun zaroori hai? Iske bina kya toot ta hai, aur kya silently toot ta hai?
Recall Solution 3.1
r kya karta hai: ek raw string banata hai, taaki Python har backslash ko matplotlib/LaTeX tak bilkul untouched pahuncha de.
r ke bina: Python pehle backslash escapes ko interpret karne ki koshish karta hai tab se pehle jab text LaTeX tak pahunche.
"\n"→ newline,"\t"→ tab,"\a"→ bell character. To"$\alpha$"ban jaata hai$+ bell +lpha$, label corrupt ho jaata hai."\lambda"→\lek recognised escape nahi hai, isliye modern Python mein DeprecationWarning aata hai lekin pass bhi ho sakta hai — yahi silent danger hai: aaj kaam karta hai, future Python mein toot jaata hai. Fix: math-label strings ko hamesharse prefix karo.
Problem 3.2
Ek reviewer kehta hai tumhare jet heatmap mein "ek edge dikh rahi hai jo data mein hai hi nahi." Brightness ke terms mein bilkul explain karo ki jet ne woh edge kaise banayi — aur viridis kyun nahi banata.
Recall Solution 3.2
Mechanism: jet blue → cyan → yellow → red chalti hai. Iska brightness (grayscale mein kitna light/dark print hota hai) yellow middle par peak tak jaata hai, phir wapas girta hai — yeh non-monotonic hai. Jahan data us value ko cross karta hai jo yellow map hoti hai, picture suddenly bright phir dark hoti hai, to tumhari aankh ek hard boundary (false edge) read karti hai, chahe data bilkul smooth ho.
Viridis kyun nahi karta: iska brightness dark purple se bright yellow tak monotonically increase karta hai. Equal data steps → equal brightness steps → koi artificial peaks nahi → koi false edges nahi. (Neeche figure dekho.)

Jet curve (burnt orange) dekho: woh beech mein hump karta hai. Viridis curve (teal) steadily chadh ta hai. Wahi hump false edge hai.
Problem 3.3
Ek line plot ke liye fig.savefig("fig.pdf") ko fig.savefig("fig.png", dpi=300) se prefer kyun kiya jaata hai, lekin ek dense photograph-jaisi heatmap ke liye ulta sach hai?
Recall Solution 3.3
Line plot → PDF (vector): vector format shapes store karta hai (yeh line point A se B tak jaati hai; yeh text yeh glyph hai). DPI irrelevant hai — infinitely zoom karo aur lines/text razor sharp rahenge. (Dekho Raster vs vector graphics.) Dense heatmap → PNG/TIFF (raster): heatmap already coloured pixels ka grid hai. Ise vector mein store karna matlab hoga per pixel ek tiny rectangle — ek gigantic file aur aksar slower rendering. Raster format high DPI par pixel grid directly aur efficiently store karta hai. Rule: shapes → vector (PDF); pixels/photos → raster (PNG/TIFF) at high DPI.
Level 4 — Synthesis
Multiple rules ko ek decision mein combine karo.
Problem 4.1
Ek journal specify karta hai: single column in, line art ke liye min dpi, body font pt. Wo rcParams + subplots + savefig block likho jo teeno knobs satisfy kare aur vector output produce kare. Har line justify karo.
Recall Solution 4.1
plt.rcParams.update({
"font.size": 9, # 9-pt body text se match karta hai
"axes.labelsize": 9, # axis labels same size
"savefig.dpi": 300, # 300-dpi minimum meet karta hai (sirf raster ke liye matter karta hai)
"savefig.bbox": "tight", # white margin trim karta hai taaki column width acche se use ho
"text.usetex": True, # exact paper fonts (final run)
})
fig, ax = plt.subplots(figsize=(3.5, 2.6)) # PHYSICAL size = column width, PEHLE set karo
# ... plot ...
ax.set_xlabel(r"$\lambda$ (nm)") # raw string taaki \lambda survive kare
fig.savefig("fig.pdf") # vector: DPI-independent line artJustification: figsize pehle inch canvas fix karta hai taaki -pt font apna true size rakhe. usetex + raw string Letters knob handle karta hai. .pdf save karna line art ke liye Dots knob handle karta hai (vector = infinite effective DPI); savefig.dpi=300 safety net hai agar koi raster element andar aa jaye.
Problem 4.2
Tumhe ek temperature anomaly field (values mein, zero = normal) heatmap ke roop mein labelled scale ke saath dikhani hai. Colormap chuno aur imshow + colorbar lines likho, bar ko LaTeX mein units ke saath label karo.
Recall Solution 4.2
Colormap choice: data ka ek meaningful center hai () → diverging → coolwarm (ya RdBu), aur symmetric limits set karo taaki neutral middle par map ho.
im = ax.imshow(Z, cmap="coolwarm", origin="lower",
vmin=-3, vmax=3, # symmetric -> 0 neutral centre par baith ta hai
extent=[x0, x1, y0, y1], aspect="auto")
cbar = fig.colorbar(im, ax=ax)
cbar.set_label(r"$\Delta T$ ($^{\circ}$C)") # units ke saath LaTeX labelvmin=-vmax kyun: symmetric limits ke bina neutral color zero se drift kar jaata hai, aur tab diverging map ka poora point khatam ho jaata hai (dekho Colormaps and color theory in visualization).
Level 5 — Mastery
Full constraints ke under debug aur design karo.
Problem 5.1
Ek colleague ki figure printed journal mein pixelated dikh rahi hai, halanki unhone dpi=300 use kiya. Unka code:
fig, ax = plt.subplots() # default figsize 6.4 x 4.8
ax.imshow(photo) # ek real photo, raster
fig.savefig("fig.png", dpi=300) # 1920 x 1440 pxJournal ise in wide print karta hai. Effective printed DPI calculate karo aur pixelation explain karo. Fix kya hai?
Recall Solution 5.1
Kya hota hai: file px wide hai. Journal ise in tak shrink karta hai, to effective DPI hai
Yeh actually high hai — to pixelation yahan printed page par kam dots ki wajah se nahi hai. Agar phir bhi blocky dikh raha hai, culprit source photo array hai: agar photo sirf, maan lo, pixels hai, to imshow ise axes fill karne ke liye upscale karta hai aur printed DPI woh detail rescue nahi kar sakta jo kabhi capture hi nahi hui thi.
Deeper case — reverse failure: agar unhone figsize=(3.5, ...) use kiya hota, to file px hoti; in par print karne par yeh exactly dpi hota — page ke liye theek, lekin risky agar source photo low-res ho.
Fix: (a) ensure karo ki source raster mein enough native pixels hain; (b) figsize ko true column width par set karo taaki file DPI = printed DPI ho aur tum directly reason kar sako.
Problem 5.2
Design goal: ek figure block, single column ( in), jo bina LaTeX installed CI server par run kare, phir bhi math labels dikhaaye, colorblind-safe sequential map use kare, aur razor-sharp line art export kare. Likho aur har choice defend karo.
Recall Solution 5.2
plt.rcParams.update({
"font.size": 9,
"axes.labelsize": 9,
"mathtext.fontset": "cm", # bina external LaTeX ke Computer-Modern-jaisa math
"text.usetex": False, # CI mein LaTeX nahi hai -> off rakhna zaroori, warna crash
"savefig.bbox": "tight",
})
fig, ax = plt.subplots(figsize=(3.5, 2.6)) # pehle column width
# ... line plot ...
ax.set_xlabel(r"$\lambda$ (nm)") # mathtext $...$ natively render karta hai
ax.set_ylabel(r"$I/I_0$")
fig.savefig("fig.pdf") # vector: sharp line art, DPI-freeDefense:
text.usetex=False+mathtext.fontset="cm"→ math phir bhi render hota hai aur LaTeX-jaisa dikhta hai, lekin matplotlib ki built-in engine use karta hai, to koi external LaTeX zaroorat nahi — CI job crash nahi karega.- Colorblind-safe sequential =
viridis/cividis(cividisstrongest colorblind safety ke liye chunao); diverging map nahi, kyunki data plain low→high hai. .pdf= vector line art, DPI-independent, to koi DPI decision hi nahi lena.figsizepehle taaki -pt font apna true size rakhe.
Recap
Recall Quick self-test
figsize=(4.0,3.0) ka pixel width 300 dpi par? ::: px.
3.5-in canvas par 1050 px ke liye min DPI? ::: dpi.
Physical printed size kaunsa knob set karta hai? ::: figsize (inches), kabhi DPI nahi.
Raw string r"$\lambda$" kyun matter karta hai? ::: Backslashes LaTeX tak untouched pahunchte hain; r ke bina Python \a, \n jaisi escapes mangle karta hai.
jet edge kaise fake karta hai? ::: Non-monotonic brightness yellow par hump karti hai, jo ek false boundary ki tarah read hoti hai.
Meaningful zero par centered data ke liye colormap? ::: Diverging (coolwarm) with symmetric vmin=-vmax.
Line plots vs dense photos ke liye format? ::: Lines ke liye PDF (vector); photos ke liye PNG/TIFF (raster, high DPI).
3.5 in par print ki gayi 1920-px file ka effective printed DPI? ::: dpi.
Connections
- Publication-quality figures — LaTeX labels, colormaps, DPI (parent)
- Matplotlib basics — figure and axes objects
- Colormaps and color theory in visualization
- LaTeX typesetting
- Raster vs vector graphics
- Dimensional analysis
- Reproducible research and rcParams