Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape special characters #181

Closed
molguin92 opened this issue Jun 15, 2017 · 1 comment
Closed

Escape special characters #181

molguin92 opened this issue Jun 15, 2017 · 1 comment

Comments

@molguin92
Copy link

matplotlib2tikz does not correctly escape special or accented characters in plots. Specifically, including characters such as the percent sign (%) in labels or captions causes a Extra }, or forgotten \endgroup. \end{tikzpicture} error; on the other hand, including accented characters such as á, é, and the like causes an inpuntenc error Package inputenc Error: Unicode char ícu (U+E5)(inputenc) not set up for use with LaTeX. \end{axis}

(Not-so Minimal) Working Example:
Python code:

def vehicles_vs_time():
    cols = ['nvhcs', 't', 'demand', 'runID']
    df = pandas.read_csv('NVeh_vs_T.csv', encoding='ISO-8859-1', sep=';')
    df.columns = cols

    df['nvhcs'] = pandas.to_numeric(df['nvhcs'])
    df['t'] = pandas.to_numeric(df['t'])
    df['runID'] = pandas.to_numeric(df['runID'])

    df['demand'] = df['demand'].map(lambda x: float(x.strip('%')) / 100)

    df100 = df.loc[df['demand'] == 1.00]
    df75 = df.loc[df['demand'] == 0.75]
    df50 = df.loc[df['demand'] == 0.50]
    df25 = df.loc[df['demand'] == 0.25]

    mean_df = pandas.DataFrame(columns=['demand', 'mean_vhcs', 'mean_time'])
    mean_df.loc[0] = [1.00, df100['nvhcs'].mean(), df100['t'].mean()]
    mean_df.loc[1] = [0.75, df75['nvhcs'].mean(), df75['t'].mean()]
    mean_df.loc[2] = [0.50, df50['nvhcs'].mean(), df50['t'].mean()]
    mean_df.loc[3] = [0.25, df25['nvhcs'].mean(), df25['t'].mean()]

    # from this point onward, plot
    fig, ax = pyplot.subplots()
    ax.set_facecolor('white')
    ax.grid(color='#a1a1a1', linestyle='-', alpha=0.1)

    pyplot.xlim([df['nvhcs'].min() - 50, df['nvhcs'].max() + 50])
    pyplot.ylim(0, df['t'].max() + 120)

    yticks_mins = numpy.arange(0, df['t'].max() + 120, 120)
    yticks_10secs = numpy.arange(0, df['t'].max() + 120, 60)

    xticks = numpy.arange(200, 1500, 100)
    xticks_minor = numpy.arange(150, 1500, 10)

    ax.set_yticks(yticks_mins)
    ax.set_yticks(yticks_10secs, minor=True)

    ax.set_xticks(xticks)
    ax.set_xticks(xticks_minor, minor=True)

    # trendline
    z = numpy.polyfit(df['nvhcs'], df['t'], 2)
    p = numpy.poly1d(z)
    nx = range(0, int(df['nvhcs'].max()) + 200)

    ax.plot(nx, p(nx), '-.', alpha=0.3, label='Ajuste polinomial', color='#F06449')

    # scatter
    ax.plot(df100['nvhcs'], df100['t'], 'o', color='#17BEBB', label='Factor de demanda 100%')
    ax.plot(df75['nvhcs'], df75['t'], 'o', color='#EF2D56', label='Factor de demanda 75%')
    ax.plot(df50['nvhcs'], df50['t'], 'o', color='#8CD867', label='Factor de demanda 50%')
    ax.plot(df25['nvhcs'], df25['t'], 'o', color='#2F243A', label='Factor de demanda 25%')
    ax.legend(loc='upper left')
    pyplot.ylabel('Tiempo (MM:SS)')

    formatter = matplotlib.ticker.FuncFormatter(to_min_secs)
    ax.yaxis.set_major_formatter(formatter)

    pyplot.xlabel('Cantidad promedio vehículos en simulación')
    # pyplot.title('Scatterplot: Cantidad promedio de vehículos vs duración en tiempo real de simulación')
    # pyplot.savefig('n_vhcs_vs_time.pgf')
    # pyplot.show()

    tikz_save('n_vhcs_vs_time.tex',
              figureheight='\\figureheight',
              figurewidth='\\figurewidth')

Generated output (note the labels and legends):

% This file was created by matplotlib2tikz v0.6.10.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.941176470588235,0.392156862745098,0.286274509803922}
\definecolor{color1}{rgb}{0.0901960784313725,0.745098039215686,0.733333333333333}
\definecolor{color2}{rgb}{0.937254901960784,0.176470588235294,0.337254901960784}
\definecolor{color3}{rgb}{0.549019607843137,0.847058823529412,0.403921568627451}
\definecolor{color4}{rgb}{0.184313725490196,0.141176470588235,0.227450980392157}

\begin{axis}[
xlabel={Cantidad promedio vehículos en simulación},
ylabel={Tiempo (MM:SS)},
xmin=150, xmax=1490,
ymin=0, ymax=1713,
width=\figurewidth,
height=\figureheight,
tick align=outside,
tick pos=left,
xmajorgrids,
x grid style={lightgray!84.183006535947712!black},
ymajorgrids,
y grid style={lightgray!84.183006535947712!black},
axis line style={white},
legend style={at={(0.03,0.97)}, anchor=north west, draw=white!80.0!black, fill=white!89.803921568627459!black},
legend cell align={left},
legend entries={{Ajuste polinomial},{Factor de demanda 100%},{Factor de demanda 75%},{Factor de demanda 50%},{Factor de demanda 25%}}
]
\addplot [semithick, color0, opacity=0.3, dash pattern=on 1pt off 3pt on 3pt off 3pt]
table {%
0 6.3172112993997
1 6.57640934437459
2 6.83677322746044
3 7.09830294865727
% ...
% ... lots of points
% ...
1611 1935.80824515293
1612 1937.94560839468
1613 1940.08413747453
1614 1942.2238323925
};
\addplot [semithick, color1, mark=*, mark size=3, mark options={solid}, only marks]
table {%
1398.4 1593
1351.4 1439
1354.67 1388
1415.13 1466
};
\addplot [semithick, color2, mark=*, mark size=3, mark options={solid}, only marks]
table {%
870.2 660
872.27 664
842.13 703
890.4 707
};
\addplot [semithick, color3, mark=*, mark size=3, mark options={solid}, only marks]
table {%
521.13 281
504.4 268
512.2 272
520.6 282
};
\addplot [semithick, color4, mark=*, mark size=3, mark options={solid}, only marks]
table {%
249.67 118
254.8 122
240.87 113
240.93 100
};
\end{axis}

\end{tikzpicture}

Expected (and working!) output:

% This file was created by matplotlib2tikz v0.6.10.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.941176470588235,0.392156862745098,0.286274509803922}
\definecolor{color1}{rgb}{0.0901960784313725,0.745098039215686,0.733333333333333}
\definecolor{color2}{rgb}{0.937254901960784,0.176470588235294,0.337254901960784}
\definecolor{color3}{rgb}{0.549019607843137,0.847058823529412,0.403921568627451}
\definecolor{color4}{rgb}{0.184313725490196,0.141176470588235,0.227450980392157}

\begin{axis}[
xlabel={Cantidad promedio veh\'iculos en simulaci\'on},
ylabel={Tiempo (MM:SS)},
xmin=150, xmax=1490,
ymin=0, ymax=1713,
width=\figurewidth,
height=\figureheight,
tick align=outside,
tick pos=left,
xmajorgrids,
x grid style={lightgray!84.183006535947712!black},
ymajorgrids,
y grid style={lightgray!84.183006535947712!black},
axis line style={white},
legend style={at={(0.03,0.97)}, anchor=north west, draw=white!80.0!black, fill=white!89.803921568627459!black},
legend cell align={left},
legend entries={{Ajuste polinomial},{Factor de demanda 100\%},{Factor de demanda 75\%},{Factor de demanda 50\%},{Factor de demanda 25\%}}
]
\addplot [semithick, color0, opacity=0.3, dash pattern=on 1pt off 3pt on 3pt off 3pt]
table {%
0 6.3172112993997
1 6.57640934437459
2 6.83677322746044
3 7.09830294865727
% ...
% ... lots of points
% ...
1611 1935.80824515293
1612 1937.94560839468
1613 1940.08413747453
1614 1942.2238323925
};
\addplot [semithick, color1, mark=*, mark size=3, mark options={solid}, only marks]
table {%
1398.4 1593
1351.4 1439
1354.67 1388
1415.13 1466
};
\addplot [semithick, color2, mark=*, mark size=3, mark options={solid}, only marks]
table {%
870.2 660
872.27 664
842.13 703
890.4 707
};
\addplot [semithick, color3, mark=*, mark size=3, mark options={solid}, only marks]
table {%
521.13 281
504.4 268
512.2 272
520.6 282
};
\addplot [semithick, color4, mark=*, mark size=3, mark options={solid}, only marks]
table {%
249.67 118
254.8 122
240.87 113
240.93 100
};
\end{axis}

\end{tikzpicture}
@nschloe
Copy link
Owner

nschloe commented Jun 15, 2017

This is a duplicate of #50, which as the time I marked as WONTFIX. The reason why I think it's difficult to avoid such errors is that LaTeX is quite complex when it comes to character transformations. One would need to write an equally complex piece of code that transforms UTF8 characters into LaTeX code, which is a humongous task, and most likely outside the scope of this project.

Eventually, the user has to make sure the characters come out right, so your plot title would have to be, e.g., 'what a nice bracket \{ \"o'.

Three advantages of this approach:

  • Less code.
  • Responsibilities are very clear.
  • The user has complete control; nothing will mess with the strings.

@nschloe nschloe closed this as completed Jun 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants