Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: panel label out of the box, above the ylabel

I have been using matplotlib for all my publication-use figures, and one question is: how to I place the panel labels that is out of the box? The tutorial is shown here, with the code and result:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
for i, label in enumerate(('A', 'B', 'C', 'D')):
    ax = fig.add_subplot(2,2,i+1)
    ax.text(0.05, 0.95, label, transform=ax.transAxes,
      fontsize=16, fontweight='bold', va='top')

plt.show()

Panel labeling

But the panel label is only at the corner of the bounding box, not out side of the box and above the y axis label (which is the usual way), like this:

Plos One format

Any input would be appreciated. Thanks!

like image 898
Yuxiang Wang Avatar asked Aug 20 '13 21:08

Yuxiang Wang


People also ask

How do I change the position of a title in Matplotlib?

Matplotlib can display plot titles centered, flush with the left side of a set of axes, and flush with the right side of a set of axes. Automatic positioning can be turned off by manually specifying the y keyword argument for the title or setting rcParams["axes.

How do you stop subplots from overlapping?

Often you may use subplots to display multiple plots alongside each other in Matplotlib. Unfortunately, these subplots tend to overlap each other by default. The easiest way to resolve this issue is by using the Matplotlib tight_layout() function.

How do I stop Matplotlib overlapping?

Use legend() method to avoid overlapping of labels and autopct. To display the figure, use show() method.


1 Answers

values outside of [0, 1] are valid.

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
for i, label in enumerate(('A', 'B', 'C', 'D')):
    ax = fig.add_subplot(2,2,i+1)
    ax.text(-0.1, 1.15, label, transform=ax.transAxes,
      fontsize=16, fontweight='bold', va='top', ha='right')

plt.show()

enter image description here

like image 125
tacaswell Avatar answered Oct 03 '22 10:10

tacaswell