I want to display a lot of graphs using matplotlib, but by default I can only get a finite number of colors. I would like to introduce an easy way to solve this. In that case, I often want to display a lot of legends, which was difficult with the old matplotlib, but now it's very easy, so I'll show you how to do it as a bonus.
For the two functions y = arctan ((n + 1) x) and y = x ^ n, we will introduce an example of overlaying a graph with n shifted by 20.
usercmap = plt.get_cmap('jet')
cNorm = colors.Normalize(vmin=0, vmax=num)
scalarMap = cm.ScalarMappable(norm=cNorm, cmap=usercmap)
Now select jet with colormap, declare color coding with 0 to num (20) number of data, and generate scalarMap with cm.ScalarMappable. In the for loop Select a color as c = scalarMap.to_rgba (i).
python
plt.legend(bbox_to_anchor=(0., 1.01, 1., 0.01), loc='lower left',ncol=10, mode="expand", borderaxespad=0.,fontsize=8)
Specify the location with bbox_to_anchor = (X coordinate, Y coordinate, X size, Y size), and adjust the number of columns to fold with ncol.
python
#!/usr/bin/env python
import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'serif'
import optparse
import matplotlib.pylab as plab
from matplotlib.colors import LogNorm
import matplotlib.colors as colors
### plot 1D plot : shifted
F = plt.figure(figsize=(12,8.))
plt.subplots_adjust(wspace=0.3, hspace=0.6)
ax = plt.subplot(2,1,1)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.figtext(0.02,0.02,"[matplotlib legend test]")
plt.xscale('linear')
plt.yscale('linear')
plt.grid(linestyle='dotted',alpha=0.5)
plt.xlabel(r'x-axis (a.u.)')
plt.ylabel(r'y-axis')
x = np.arange(-1,1,0.1)
num=20
y = []
for j in range(num):
y.append(np.arctan(x*(j+1)))
usercmap = plt.get_cmap('jet')
cNorm = colors.Normalize(vmin=0, vmax=num)
scalarMap = cm.ScalarMappable(norm=cNorm, cmap=usercmap)
for i,oney in enumerate(y):
c = scalarMap.to_rgba(i)
plt.errorbar(x, oney, fmt="o-", color = c, alpha = 0.8, linewidth = 1, label=str(i))
plt.legend(bbox_to_anchor=(0., 1.01, 1., 0.01), loc='lower left',ncol=10, mode="expand", borderaxespad=0.,fontsize=8)
ax = plt.subplot(2,1,2)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.xscale('linear')
plt.yscale('linear')
plt.grid(linestyle='dotted',alpha=0.5)
plt.xlabel(r'x-axis (a.u.)')
plt.ylabel(r'y-axis')
x = np.arange(-1,1,0.1)
num=20
y = []
for j in range(num):
y.append(np.power(x,j+1))
usercmap = plt.get_cmap('jet')
cNorm = colors.Normalize(vmin=0, vmax=num)
scalarMap = cm.ScalarMappable(norm=cNorm, cmap=usercmap)
for i,oney in enumerate(y):
c = scalarMap.to_rgba(i)
plt.errorbar(x, oney, fmt="o-", color = c, alpha = 0.8, linewidth = 1, label=str(i))
plt.legend(bbox_to_anchor=(-0.1, 1.0, 1.2, 0.01), loc='lower left',ncol=10, mode="expand", borderaxespad=0.,fontsize=8)
plt.savefig("qiita_legend_sample.png ")
plt.show()
Click here for the generated figure.
It's now easier to expand the legend horizontally and display it outside the graph, but the expand option only supports horizontal, not vertical (as of 2019.1.8). I think time will be settled.
Recommended Posts