Skip to content

Commit 5f4f7a4

Browse files
dougndnschloe
authored andcommitted
Prevent empty Line2D from creating blank tables (#134)
Empty Line2D objects will create empty PGFPlot addplot tables. These tables are interpreted as being external data tables from file '' or '.txt' which can cause Latex to crash. See #134
1 parent c0559d4 commit 5f4f7a4

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

matplotlib2tikz/line2d.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ def draw_line2d(data, obj):
1010
content = []
1111
addplot_options = []
1212

13+
# If line is of length 0, do nothing. Otherwise, an empty \addplot table
14+
# will be created, which will be interpreted as an external data source
15+
# in either the file '' or '.tex'. Instead, render nothing.
16+
if len(obj.get_xdata()) == 0:
17+
return data, []
18+
1319
# get the linewidth (in pt)
1420
line_width = _mpl_linewidth2pgfp_linewidth(data, obj.get_linewidth())
1521

test/testfunctions/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
__all__ = [
44
'annotate',
55
'basic_sin',
6+
'boxplot',
67
'dual_axis',
78
'errorband',
89
'errorbar',

test/testfunctions/boxplot.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding: utf-8 -*-
2+
""" Box Plot test
3+
4+
This test plots a box plot with three data series. The causes an empty Line2D
5+
to be plotted. Without care, this can turn into an empty table in PGFPlot
6+
which crashes latex (due to it treating an empty table as a table with
7+
external data in the file '' or '.tex')
8+
See: https://github.com/nschloe/matplotlib2tikz/pull/134
9+
"""
10+
desc = 'BoxPlot'
11+
phash = '6bc6f6b95e0000fe'
12+
13+
def plot():
14+
import matplotlib.pyplot as plt
15+
16+
# plot data
17+
fig = plt.figure()
18+
ax = fig.add_subplot(111)
19+
20+
data = [[0.8792419963142024, 0.8842648555256405, 0.8830545971510088, 0.8831310510125482, 0.8839926059865629, 0.8795815040451961, 0.8780455489941472, 0.8785436398314896, 0.8830947020953477, 0.8853267660041949, 0.8888678711018956, 0.8852975957910832, 0.8806832729996307, 0.8757157004574541, 0.8767001155960863, 0.8840806038864472, 0.8817619814119265, 0.8888364252374024, 0.8812448127688732, 0.8831027782255365], [0.8977874209274417, 0.8941751386130553, 0.8896779411432865, 0.8971274869048325, 0.8974081692527065, 0.8942767272739647, 0.8875248054826029, 0.8777267389916926, 0.8950411839136605, 0.8927553406630346, 0.8950822278376636, 0.8987940094730611, 0.8921713177345106, 0.8875512496817447, 0.8897284821652239, 0.8910385725900226, 0.8879321741542129, 0.889056167587369, 0.884905350828982, 0.89214934207348], [0.8841888415170959, 0.8922931655807687, 0.8896153674950393, 0.8875992162118492, 0.890776178375901, 0.8889109386518265, 0.8879119743598638, 0.8912870099488378, 0.8981046527087161, 0.8920725720963792, 0.8841683225315845, 0.8857539590587772, 0.8945156112818913, 0.8894879283167035, 0.8912651966639861, 0.8929190818922158, 0.8943297597492411, 0.8888594626359189, 0.8912494597675972, 0.8917524004164856]]
21+
22+
23+
ax.boxplot(data)
24+
25+
return fig
26+

0 commit comments

Comments
 (0)