1 Pseudo-color map with polar projection

We usually need to plot pseudo-color maps in polar projection besides except of those in Cartesian projection. Setting the projection as polar in method subplot. Here , we give an example of python code.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import numpy as np
import matplotlib.pyplot as plt

theta = np.linspace(0, 2*np.pi, 121)
r = np.linspace(0, 10, 51)

T, R = np.meshgrid(theta, r)
v = np.sin(T*R) * np.exp(-(T**2+R**2)/25)

plt.figure(figsize=(6, 4.5))
plt.subplot(111, projection='polar')
plt.pcolormesh(theta, r, v, cmap='CMRmap')
plt.colorbar(shrink=0.5)
plt.tight_layout()
plt.show()

plot_fig01

2 Color bar, tick, axis, grid line and frame settings

2.1 Color bar

shrink: Relative length of color bar;
aspect: The ratio of height (length) to width;
pad: The distance between map and color bar.

1
2
3
cbar = plt.colorbar(shrink=1, aspect=30, pad=0.075)
cbar.set_label(r'Name string', fontsize=15)
cbar.ax.tick_params(labelsize=13)

Example:`

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import numpy as np
import matplotlib.pyplot as plt

n = 101
x = np.linspace(-5, 5, n)
y = x.copy()
X, Y = np.meshgrid(x, y)
z = np.sin(X**2+Y**2)

plt.figure(figsize=(6, 4.5))
plt.pcolormesh(x, y, z, cmap='coolwarm')
cbar = plt.colorbar(shrink=0.75, aspect=30, pad=0.075, extend='both')
cbar.set_label(r'Amplitude', fontsize=15)
cbar.ax.tick_params(labelsize=13)
plt.tight_layout()
plt.show()

plot_fig02

2.2 Ticks

  • fontsize: Size of tick fonts;

  • rotation: Rotation angle of tick fonts;

    1
    
    plt.xticks(fontsize=12, rotation=-90)
    
  • Setting tick strings:

    1
    2
    3
    4
    
    t = np.linspace(0, 10, 101)
    s = np.sin(s)
    plt.plot(t, s)
    plt.xticks(np.arange(1, 10, 2), ['I', 'III', 'V', 'VII', 'IX'])
    
  • The directions of ticks:

    1
    2
    
    plt.rcParams['xtick.direction'] = 'in' # 'out' for outward
    plt.rcParams['ytick.direction'] = 'in' # 'out' for outward
    
  • The positions of ticks:

    1
    2
    
    ax.xaxis.set_ticks_position('top') # Put the x ticks on the top side.
    ax.yaxis.set_ticks_position('right') # Put the y ticks on the right side.
    

    Example:`

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    n = 101
    x = np.linspace(0, 4*np.pi, n)
    y = np.sin(x)
      
    plt.figure(figsize=(6, 3))
    plt.plot(x, y, lw=2, color='b')
    plt.xticks(np.arange(5)*np.pi, ['0', r'$\pi$', r'$2\pi$', r'$3\pi$', r'$4\pi$'])
    plt.rcParams['xtick.direction'] = 'in'
    plt.rcParams['ytick.direction'] = 'in'
    ax = plt.gca()
    ax.xaxis.set_ticks_position('top') # Put the x ticks on the top side.
    ax.yaxis.set_ticks_position('right') # Put the y ticks on the right side.
    plt.tight_layout()
    plt.show()
    

    plot_fig03

2.3 Label

  • color: Color of label;
  • rotation: Rotation angle of the label;
  • fontsize: Font size of the label.
  • label position ax.yaxis.set_label_position('right') or ax.xaxis.set_label_position('top')
1
plt.xlabel('X Label', fontsize=25, color='b', rotation=-30)

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
n = 101
x = np.linspace(0, 4*np.pi, n)
y = np.sin(x)

plt.figure(figsize=(6, 3))
plt.plot(x, y, lw=2, color='b')
plt.xlabel('Time (s)', rotation=-30, fontsize=15, color='r')
plt.ylabel('Amplitude', rotation=45, fontsize=15, color='g')
plt.tight_layout()
plt.show()

plot_fig04

2.4 Axes

  • plt.gca().invert_xaxis() or plt.gca().invert_yaxis(): Invert x or y axis;
  • Setting frames: visible (True) or invisible (False):
1
2
3
4
5
fig, ax = plt.subplots()
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
  • The position of axes:

    ax.xaxis.set_label_position('top'): Setting 'bottom' to put it on the bottom side; ax.yaxis.set_label_position('right'): Setting 'left' to put it on the left side.

  • The width of the frame:

    1
    2
    3
    4
    5
    6
    
    bwidth = 2
    ax = plt.gca()
    ax.spines['bottom'].set_linewidth(bwidth)
    ax.spines['left'].set_linewidth(bwidth)
    ax.spines['top'].set_linewidth(bwidth)
    ax.spines['right'].set_linewidth(bwidth)
    
  • The color of the frame:

    1
    2
    3
    4
    
    ax.spines['left'].set_color('r')
    ax.spines['top'].set_color('b')
    ax.spines['right'].set_color('r')
    ax.spines['bottom'].set_color('b')
    

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
n = 101
x = np.linspace(0, 4*np.pi, n)
y = np.sin(x)

plt.figure(figsize=(6, 3))
plt.plot(x, y, lw=2, color='gray')
ax = plt.gca()
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(True)
ax.spines['left'].set_visible(True)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('r')
ax.spines['bottom'].set_color('b')
bwidth = 2
ax.spines['bottom'].set_linewidth(bwidth)
ax.spines['left'].set_linewidth(bwidth)
plt.tight_layout()
plt.show()

plot_fig06

2.5 Grid lines

  • Major grid lines.
1
plt.grid(b=True, which='major', color='r', alpha=0.5, linewidth=1,  visible=True, ls='--')
  • Minor grid lines.
1
plt.grid(b=True, which='minor', color='g', alpha=0.5, linewidth=1,  visible=True, ls=':')
  • Setting grid lines with your style.
1
2
3
4
ax.yaxis.grid(ls=':', color='r')
ax.set_yticks(np.linspace(-1, 1, 21))
ax.xaxis.grid(ls='--', color='g')
ax.set_xticks(np.linspace(0, 10, 21))
  • Settings of the figure frame.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    
    bwidth = 2
    ax = plt.gca()
    # Set the width of the frame.
    ax.spines['bottom'].set_linewidth(bwidth)
    ax.spines['left'].set_linewidth(bwidth)
    ax.spines['top'].set_linewidth(bwidth)
    ax.spines['right'].set_linewidth(bwidth)
    # Set the frame color.
    ax.spines['bottom'].set_color('#666666')
    ax.spines['left'].set_color('#333333')
    ax.spines['right'].set_color('#333333')
    ax.spines['top'].set_color('#333333')
    

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
n = 101
x = np.linspace(0, 4*np.pi, n)
y = np.sin(x)

plt.figure(figsize=(6, 4))
plt.plot(x, y, lw=2, color='gray')
ax = plt.gca()
ax.yaxis.grid(ls=':', color='r')
ax.xaxis.grid(ls='--', color='g')
plt.tight_layout()
plt.show()

plot_fig07

2.6 Subplot alignment

ax = plt.subplot2grid((r, c), (rp, cp), rowspan=nr, colspan=nc)

r: rows of the whole figure;

c: columns of the whole figure;

rp: the start row index of the sub-figure, and it must be lower than r;

cp: the start column index of the current sub-figure, and it must be lower than c;

nr: the row number of the current sub-figure;

nc: the column number of the current sub-figure.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
x = np.linspace(0, 10, 101)
y = np.sin(5*x) * np.exp(-(x-5)**2)

r = 6; c = 6
plt.figure(figsize=(6, 5))
ax1 = plt.subplot2grid((r, c), (0, 0), rowspan=1, colspan=c)
ax1.plot(x, y, color='r')

ax2 = plt.subplot2grid((r, c), (1, 0), rowspan=2, colspan=2)
ax2.plot(x, y, color='g')

ax3 = plt.subplot2grid((r, c), (1, 2), rowspan=1, colspan=2)
ax3.plot(x, y, color='b')

ax3 = plt.subplot2grid((r, c), (2, 2), rowspan=1, colspan=2)
ax3.plot(x, y, color='m')

ax4 = plt.subplot2grid((r, c), (3, 0), rowspan=3, colspan=4)
ax4.plot(x, y, color='c')

ax5 = plt.subplot2grid((r, c), (1, 4), rowspan=r-1, colspan=2)
ax5.plot(y, x, color='k')

plt.tight_layout()
plt.show()

Fig08

3 Background, error bars

background

1
plt.style.use('dark_background')

Error bars

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 5, 26)
y = np.sin(x)
plt.errorbar(x, y, yerr=0.2, marker='s', color='g',
            markersize=10, markerfacecolor='k', markeredgecolor='w',
            lw=2, ls=':', ecolor='r', elinewidth=1, capsize=3, capthick=2.5,
            label='style1')
plt.errorbar(x, y+1, yerr=0.1, color='g', lw=1, ls='-',
            ecolor='r', elinewidth=1, capsize=3, capthick=2.5,
            label='style2')
plt.errorbar(x, y+2, yerr=np.random.randn(len(y))*0.1, color='g', lw=1, ls='-',
            ecolor='r', elinewidth=1, capsize=3, capthick=2.5, label='style3')
plt.legend()
plt.show()

error bar

4 Inset a smaller figure

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import numpy as np
import matplotlib.pyplot as plt
#from mpl_toolkits.axes_grid1.inset_locator import mark_inset
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

x = np.linspace(0, 5, 201)
y = np.sin(x**2)

fig, ax = plt.subplots(1, 1, figsize=(10, 6))
ax.plot(x, y, 'r', lw=2)
axins = inset_axes(ax, width="30%", height="30%", loc='lower left',
                   bbox_to_anchor=(0.05, 0.05, 1, 1),
                   bbox_transform=ax.transAxes)
axins.plot(x, y, color='b', lw=2)
plt.show()

inset figures

5 Remove the withe grid lines in saved PDF file

1
plt.pcolormesh(bazz, sloww*1e3, PP, cmap='CMRmap', linewidth=0,rasterized=True)

6 Make a color map with your own colors

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import matplotlib as mpl

cols =  '#EEEEEE    #E9EAEF    #E5E6F0    #E2E3F0    #E0DDEF    #DCDAEF'\
	  + '#D9D8F0    #D4D4F0    #D0D1F1    #CDCCF0    #CBCAF1    #C5C6F1'\
	  +	'#C1C2F2    #C0C1F2    #AEADEE    #9F9FF2    #9292F5    #8786F8'\
	  + '#7272F9    #6261F5    #5959F8    #4544FA    #3434F6    #2827F9'\
	  + '#1618F9    #0100FF    #0f0eEF    #1817E4    #2F2FCE    #4647B4'\
	  + '#5251A9    #6B6A92    #7D7D7D    #929567    #9EA058    #B7B645'\
	  + '#CDCF2E    #D8D424    #EDF107    #FFF802    #FEE601    #FEDB00'\
	  + '#FDC800    #FDB500    #FFAC02    #FE9700    #FF8402    #FD7700'\
	  + '#FF6B00    #FF5602    #FE4800    #FF3E01    #FC2C00    #FF2203'\
	  + '#FF2103    #FE1F00    #FE1A00    #FE1600    #FE1400    #FE1000'\
	  + '#FE0c00    #FE0a00    #FD0700    #FD0200'
cmap = mpl.colors.LinearSegmentedColormap.from_list('cmap', cols.split(), 101)

7 Text with the $LaTeX$ style

1
2
3
4
import matplotlib

# Set True for using the LaTeX style.
matplotlib.rcParams['text.usetex'] = True