3

When working with MatPlotLib I constantly find myself creating a plot, and some time later (could be years) I realize that I'm not fully satisfied with a certain detail in my plot. This could be things like:

  • axis ranges
  • axis labels, tick labels, tick placing, grid, ...
  • all font sizes (ticks, labels, legend)
  • plot coloring, choice of markers
  • plot annotation, for instance, I want to manually comment on the plot etc

My question is whether there is an approach that makes applying such modifications convenient?

Currently every modification means that I have to completely regenerate the whole plot anew, just with a minor adjustment in to plotting logic. This is certainly doable (and in some cases preferable!) but I always have to store the whole underlying data, the whole plotting logic, and some (of my own) modules (since I'm afraid I might break compatibility at some point in the future).

I'm wondering whether it is possible to simply persist/serialize the full plotting state. In other words: instead of storing the logic that generates a plot, can I simply take the "whole MatPlotLib state", write it disk, and restore the state later in order to apply minor modifications (maybe even interactively in iPython)?

bluenote10
  • 23,414
  • 14
  • 122
  • 178
  • Same Problem here - I don't know any method for savings plots for later modification either. I use two approaches to circumvent this problem: a) serialize the raw data and have separate the processing and plot generation. b) for visual changes inkscape works quite well on saved PDFs or SVGs – Dietrich Jan 31 '14 at 12:21
  • have you tried pickling the figure? – Matti Lyra Jan 31 '14 at 12:22
  • @Dietrich: My solution is pretty similar: I split into (1) raw data (2) small plot script, and (3) small library with common plotting functions. As a result of my laziness of re-plotting I became quite proficient with Inkscape as well :). – bluenote10 Jan 31 '14 at 12:48
  • There is not (to my knowledge) any support for this directly in matplolib. My personal approach is the same as your, a script that generates the plot and never modify a plot I intend to save interactively. This would be an interesting thing to add to the emerging thoughts about semantic objects in mpl. – tacaswell Jan 31 '14 at 14:19

1 Answers1

0

per http://cs109.org/

%matplotlib inline

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

#tell pandas to display wide tables as pretty HTML tables
pd.set_option('display.width', 500)
pd.set_option('display.max_columns', 100)

def remove_border(axes=None, top=False, right=False, left=True, bottom=True):
    """
    Minimize chartjunk by stripping out unnecesasry plot borders and axis ticks

    The top/right/left/bottom keywords toggle whether the corresponding plot border is drawn
    """
    ax = axes or plt.gca()
    ax.spines['top'].set_visible(top)
    ax.spines['right'].set_visible(right)
    ax.spines['left'].set_visible(left)
    ax.spines['bottom'].set_visible(bottom)

    #turn off all ticks
    ax.yaxis.set_ticks_position('none')
    ax.xaxis.set_ticks_position('none')

    #now re-enable visibles
    if top:
        ax.xaxis.tick_top()
    if bottom:
        ax.xaxis.tick_bottom()
    if left:
        ax.yaxis.tick_left()
    if right:
        ax.yaxis.tick_right()
o-90
  • 17,045
  • 10
  • 39
  • 63
  • 3
    I'm not entirely sure if I understand this answer correctly. I guess it is an example of "applying modifications to `axes` after a plot". My question was more whether it is possible to persist an "axes state" either be serializing or by conveniently storing all "axes modified instructions". – bluenote10 Feb 02 '14 at 11:46