Skip to content

Style configuration changing behavior of savefig #9671

Closed

Description

@jasonrobwebster

Hi,

I recently upgraded to Matplotlib 2.1.0 and for some reason my fig.savefig functionality has changed. Basically, it now requires a bytes-like object whenever a certain style file is used. Running the following code:

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('aps')

x = np.linspace(0, 1, 100)
y = x**2

plt.plot(x, y)
fig = plt.gcf()

fig.savefig('test.pdf')

with the style configuration file 'aps' set to:

font.family	: serif
font.serif	: Times
font.size	: 8.0

text.usetex	: True

xtick.labelsize	: 8
xtick.direction	: in
xtick.major.size: 3

ytick.labelsize	: 8
ytick.direction	: in
ytick.major.size: 3

axes.labelsize	: 8
axes.titlesize	: 8

legend.fontsize	: 7

lines.linewidth	: 1
lines.markersize: 2

figure.figsize	: 3.417, 2.112
figure.dpi	: 100

figure.subplot.left	: 0.133
figure.subplot.right	: 0.99
figure.subplot.top	: 0.97
figure.subplot.bottom	: 0.16

savefig.dpi	: 600

I get

TypeError: a bytes-like object is required, not 'str'

at the line that involves the fig.savefig line. Weirdly, removing plt.style.use('aps') removes this error (but gives me the ugly default style).

Fixing this is simple enough on my side (replace the string with the bytes like obj), but this shouldn't be occurring in fig.savefig - thought I would bring it up.

Important info:
MPL version: 2.1.0
System: Windows 7 Ultimate
Tex backend is MikTex 2.9

Python version:
'3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]'

Regards,
jrw

Activity

  1. jasonrobwebster commented on Nov 2, 2017

    @jasonrobwebster
    Author

    I jumped the gun, fixing it is not as simple as I thought. Using

    fp = open('test.pdf', 'wb')
    
    fig.savefig(fp)
    
    fp.close()

    executes properly but the pdf does not open. Changing the extension to png does work (and will even allow fig.savefig to accept a string).

  2. jklymak commented on Nov 2, 2017

    @jklymak
    Member

    Just for fun, remove the usetex line... Not saying you should have to do that, but its the only thing in there that looks like it could cause a problem?

  3. jasonrobwebster commented on Nov 2, 2017

    @jasonrobwebster
    Author

    That fixes it in so far as I can save a pdf file (and I can use savefig with a str input). Messes up the point of the style file though.

  4. jklymak commented on Nov 2, 2017

    @jklymak
    Member

    Can you modify the Issue with the operating system, versions of MPL, python etc, libraries and tex version you are using? I can't help w/ this (above my pay grade) but its likely necessary info.

    FWIW, it works for me, so its something in your setup...

    test

  5. WeatherGod commented on Nov 2, 2017

    @WeatherGod
    Member
  6. jklymak commented on Nov 2, 2017

    @jklymak
    Member

    Ah, maybe thats it. I'm working off master...

  7. jasonrobwebster commented on Nov 2, 2017

    @jasonrobwebster
    Author

    Ah okay, so whatever it was seems to be patched up. I'm working off the latest stable build from PyPi. I've downgraded to Matlab 2.0.2 for now, at least til an official stable patch gets released.

  8. jklymak commented on Nov 2, 2017

    @jklymak
    Member

    Closed by #9292 Thanks for the report @jasonrobwebster and sorry I didn't remember that it'd been fixed earlier...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions