san04
san04

Reputation: 1

Font issue in ticklabel with pandas bar plot with pgf output for latex

Using python (3.12.4) to produce a pgf output for a bar plot with this code

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

plt.rc('font',**{'family':'serif','serif':['Palatino'], 'size': 20})
plt.rc('text', usetex=True)
fig, ax = plt.subplots()


df = pd.DataFrame({'a':[1,3,4,2], 'b':[1,2,3,4]})
df.plot.bar(x='a', y='b', ax=ax)
plt.xticks(rotation=0)

plt.savefig(f'out.pgf',bbox_inches='tight')

and compiling it with pdflatex (pdflatex --version pdfTeX 3.141592653-2.6-1.40.26)

\documentclass{standalone}
\usepackage{palatino}
\usepackage{tikz}
\usepackage{amsmath}
\def\mathdefault#1{#1}
\begin{document}
\input{out.pgf}
\end{document}

results in different font types on x- and y-ticklabels:

Plot

I would expect the same fonttype. Is this a bug, or are there workarounds?

Upvotes: 0

Views: 40

Answers (1)

san04
san04

Reputation: 1

Okay, manually introducing \mathdefault{} to every label on the x-axis at least produces the desired output.

There might even exit code snippets less readable than this (maybe even not):

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

plt.rc('font',**{'family':'serif','serif':['Palatino'], 'size': 20})
plt.rc('text', usetex=True)
fig, ax = plt.subplots()


df = pd.DataFrame({'a':[1,3,4,2], 'b':[1,2,3,4]})
df.plot.bar(x='a', y='b', ax=ax)
plt.xticks(rotation=0)

labels=ax.get_xticklabels()
for i,_ in enumerate(labels):
    labels[i].set_text(f'$\\mathdefault{{{labels[i].get_text()}}}$')
ax.set_xticklabels(labels)


plt.savefig(f'out.pgf',bbox_inches='tight')

Simpler solutions are always welcome.

Upvotes: 0

Related Questions