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

Pandas plot method in connection with plt.fill_between causes Python to crash #12888

Open
bjonen opened this issue Apr 13, 2016 · 2 comments
Open
Labels
Bug Datetime Datetime data dtype Visualization plotting

Comments

@bjonen
Copy link
Contributor

bjonen commented Apr 13, 2016

When creating a plot with two y-axis, I run into the following problem. When plotting using the pd.Series.plot() method on the first y-axis and then applying ax.fill_between() Python crashes. If I use fill_between on the primary y-axis also the problem disappears.

Code Sample, a copy-pastable example if possible

npers = 100
index = pd.date_range(start='2000', freq='BM', periods=npers)
rel = pd.Series(index=index, data=np.random.randn(npers))
dd = pd.Series(index=index, data=np.random.randn(npers))

# this works
(w, h) = plt.figaspect(0.6)
fig = plt.figure(figsize=(w, h))
ax = fig.add_subplot(111)
# Not using pd.Series.plot()
ax.plot(rel.index, rel.values)
ax2 = ax.twinx()
ax2.fill_between(dd.index, dd.values)

# this crashes Python
(w, h) = plt.figaspect(0.6)
fig = plt.figure(figsize=(w, h))
ax = fig.add_subplot(111)
# Using pd.Series.plot()
ax = rel.plot(ax=ax)
ax2 = ax.twinx()
ax2.fill_between(dd.index, dd.values)

Expected Output

image

output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 2.7.11.final.0
python-bits: 64
OS: Windows
OS-release: 8.1
machine: AMD64
processor: Intel64 Family 6 Model 69 Stepping 1, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.18.0
nose: 1.3.7
pip: 8.1.1
setuptools: 20.6.7
Cython: None
numpy: 1.11.0
scipy: 0.17.0
statsmodels: 0.6.1
xarray: None
IPython: 4.0.0
sphinx: 1.3.1
patsy: 0.3.0
dateutil: 2.5.2
pytz: 2016.3
blosc: None
bottleneck: None
tables: 3.1.1
numexpr: 2.3.1
matplotlib: 1.5.1
openpyxl: 1.8.5
xlrd: 0.9.3
xlwt: 1.0.0
xlsxwriter: None
lxml: 3.5.0
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 0.7.9
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.9.4

@TomAugspurger
Copy link
Contributor

I don't get a crash, but I do see this

gh

IIRC we put additional state on the axes object for timeseries plots, which presumably isn't copied over correctly when the axis is twinned. I'm not sure the best way to handle it.

Easiest workaround for now is either what you did first, or use the x_compat=True parameter in the first call to rel.plot(...). That gives me a correct plot. Or you can change your second plot to ax2 = dd.plot(ax=ax, secondary_y=True) and then fill_between on that.

@TomAugspurger TomAugspurger added this to the 0.18.2 milestone Apr 13, 2016
@bjonen
Copy link
Contributor Author

bjonen commented Apr 14, 2016

Thanks, both your work-arounds work for me. I am using this one now:

(w, h) = plt.figaspect(0.6)
fig = plt.figure(figsize=(w, h))
ax = fig.add_subplot(111)
ax = rel.plot(ax=ax)
ax2 = dd.plot(ax=ax, secondary_y=True, alpha=0.)
ax2.fill_between(dd.index, dd.values)

@jorisvandenbossche jorisvandenbossche modified the milestones: Next Major Release, 0.19.0 Aug 15, 2016
@mroeschke mroeschke added the Bug label Apr 23, 2021
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Visualization plotting
Projects
None yet
Development

No branches or pull requests

5 participants