nschloe/tikzplotlib

Too many redundant points in generated tables

Saltsmart opened this issue · 1 comments

Please provide a minimal working example (MWE) that shows what isn't working as
expected. Needed is

  • the Python code,
  • the output TeX (Pgfplots), and
  • the expected TeX (Pgfplots).

Perhaps the Pgfplots manual
is helpful here.

The Python Code

I‘ve prepared a function to plot the figure for 2 vars (columns) in a pandas DataFrame:

def plot_vs_in_units(data, variables, labels, width=12, height=None, labelsize=17, name=None):
    """Plot data variable X versus Y in a figure for each unit.

    Args:
        data (DataFrame): Pandas DataFrame.
        variables (list): Specifies which pair of vars (in format "[strX, strY]") are plotted.
        labels (list): Specifies X and Y labels (in format "[strX, strY]").
        width (int, optional): Width for the figure. Defaults to 12.
        height (int or None, optional): Height for the figure, = 0.75 * width if None is specified. Defaults to None.
        labelsize (int, optional): X and Y label size. Defaults to 17.
        name (str or None, optional): File name for saving, not to save if None is specified. Defaults to None.
    """
    plt.clf()
    leg = []
    if height is None:
        plt.figure(figsize=(width, 0.75 * width))
    else:
        plt.figure(figsize=(width, height))

    unit_sel = np.unique(data["unit"])
    for i in unit_sel:
        data_unit = data.loc[data["unit"] == i]
        plt.plot(data_unit[variables[0]], data_unit[variables[1]], "-o", color=color_dic_unit["Unit " + str(int(i))],
                 alpha=0.7, markersize=1)
        leg.append("Unit " + str(int(i)))
    plt.tick_params(axis="x", labelsize=labelsize)
    plt.tick_params(axis="y", labelsize=labelsize)
    plt.xlabel(labels[0], fontsize=labelsize)
    plt.ylabel(labels[1], fontsize=labelsize)
    plt.legend(leg, fontsize=labelsize - 4, loc="best")

    plt.tight_layout()
    if name is not None:
        tikzplotlib.clean_figure()
        tikzplotlib.save(name + ".tex")
        plt.savefig(name + ".png", format="png")
    plt.show()
    plt.close()

The output TeX (Pgfplots)

The output tex file has two many identical points in the table. It makes Overleaf fails to compile:

% This file was created with tikzplotlib v0.10.1.
\begin{tikzpicture}

\definecolor{crimson2143940}{RGB}{214,39,40}
\definecolor{darkgray176}{RGB}{176,176,176}
\definecolor{darkorange25512714}{RGB}{255,127,14}
\definecolor{darkturquoise23190207}{RGB}{23,190,207}
\definecolor{gray127}{RGB}{127,127,127}
\definecolor{lightgray204}{RGB}{204,204,204}
\definecolor{mediumpurple148103189}{RGB}{148,103,189}
\definecolor{sienna1408675}{RGB}{140,86,75}
\definecolor{steelblue31119180}{RGB}{31,119,180}

\begin{axis}[
legend cell align={left},
legend style={
  fill opacity=0.8,
  draw opacity=1,
  text opacity=1,
  at={(0.03,0.97)},
  anchor=north west,
  draw=lightgray204
},
tick align=outside,
tick pos=left,
x grid style={darkgray176},
xlabel={RUL [-]},
xmin=-4.4, xmax=92.4,
xtick style={color=black},
y grid style={darkgray176},
ylabel={\(\displaystyle h_s\) [-]},
ymin=-0.05, ymax=1.05,
ytick style={color=black}
]
\addplot [semithick, darkorange25512714, opacity=0.7, mark=*, mark size=0.5, mark options={solid}]
table {%
74 1
74 1
...

The tex file has 19,999+ lines of "74 1" for example. The file size reachs 37 MB.

The expected TeX (Pgfplots)

I hope tikzplotlib can get rid of these redundant points so I can compile it.
The figure is showed normally by plt.show(). It's pretty simple:
RUL_hs

That's because too many redundant points are in the original matplotlib figure.