Double linear error not working with matplotlib and xkcd style

The following python3 code does not work due to double linebreak on line 9:

# -*- coding: utf-8 -*- from matplotlib import pyplot as plt import numpy as np plt.xkcd() fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') plt.text(4, 400, '-> 1 Pig ~ 150 kg\n\n-> Butching => 80 to 100 kg meat') plt.axis([0, 7, 0, 2000]) plt.plot([0,1,2,3,4,5], [0,400,800,1200,1600, 2000]) ax.set_ylim([0, 2000]) ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') plt.show() 

But if I delete the line plt.xkcd() , then everything will work well even with double-line decoupling. Anyone now why? Is this a bug or is there a workaround?

My setup: Windows 7 amd64, python 3.3, numpy 1.8, matplotlib 1.3.1

+6
source share
1 answer

Two hacks to fix this:

  • replace the double newline with "\ n. \ n" (ie add a small dot)

     plt.text(4, 400, '-> 1 Pig ~ 150 kg\n.\n-> Butching => 80 to 100 kg meat') 
  • Divide multiline text into multiple text calls (best result)

     plt.text(4, 400, '-> 1 Pig ~ 150 kg') plt.text(4, 240, '-> Butching => 80 to 100 kg meat') 

    or

     text = '-> 1 Pig ~ 150 kg\n\n-> Butching => 80 to 100 kg meat' for il, l in enumerate(text.split('\n')): plt.text(4, 400-80*il, l) 
+1
source

All Articles