GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — [Bug]: LaTeX X-Tick Labels are Vertically Misaligned When DPI is Set · Issue #31802 · matplotlib/matplotlib Open DescriptionBug summaryx-tick labels using LaTeX are vertically misaligned when you call Code for reproductionimport sys import matplotlib import matplotlib.pyplot as plt print(matplotlib.__version__) print(matplotlib.get_backend()) print(sys.version) idxs = list(range(3)) labels = ["$w^{(2)}_1$", "$w^{(2)}_2$", "$b^{(2)}$"] fig, ax = plt.subplots(figsize=(3, 3)) ax.set_xlim(-0.5, 2.5) ax.set_ylim(-3, 3) ax.set_xticks(idxs, labels) ax.set_yticks([]) ax.tick_params(which="both", length=0) fig.tight_layout() fig.savefig("test.png", dpi=300) Actual outcomeHere is the image produced by the code. You can see that the
Expected outcomeHere is the same image if you run the code without the
Additional information[EDIT: slight fix to code] Here's the reproduction code modified to use a workaround: import sys import matplotlib import matplotlib.pyplot as plt from matplotlib.artist import Artist from matplotlib.figure import Figure print(matplotlib.__version__) print(matplotlib.get_backend()) print(sys.version) def get_center(fig: Figure, artist: Artist) -> tuple[float, float]: renderer = fig.canvas.get_renderer() # type: ignore # get_renderer() not in base class bbox = artist.get_window_extent(renderer) fig_box = bbox.transformed(fig.transFigure.inverted()) x = (fig_box.xmin + fig_box.xmax) / 2 y = (fig_box.ymin + fig_box.ymax) / 2 return x, y idxs = list(range(3)) labels = ["$w^{(2)}_1$", "$w^{(2)}_2$", "$b^{(2)}_1$"] # add subscript to b2 fig, ax = plt.subplots(figsize=(3, 3)) ax.set_xlim(-0.5, 2.5) ax.set_ylim(-3, 3) ax.set_xticks(idxs, labels) ax.set_yticks([]) ax.tick_params(which="both", length=0) fig.tight_layout() # call the layout function before fixing the label orig_b2_label = ax.get_xticklabels()[-1] orig_b2_x, orig_b2_y = get_center(fig, orig_b2_label) # measure while it's still visible orig_b2_label.set_visible(False) b2_label = fig.text( orig_b2_x, orig_b2_y, "$b^{(2)}$", ha="center", va="center", transform=fig.transFigure ) b2_label.set_in_layout(False) fig.savefig("test-workaround.png", dpi=300) Here is the image with the workaround:
(Feel free to let me know if there is a better workaround.) Operating systemUbuntu 24 Matplotlib Version3.10.9 Matplotlib Backendmodule://backend_interagg Python versionPython 3.12.3 Jupyter versionNo response Installationuv Activity
MetadataMetadataAssigneesTypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |