LoginSignup
2
4

[Matplotlib]複数グラフでcolorbarを共有する

Last updated at Posted at 2023-07-24

表題のままです.
少し面倒だったのでメモ.

下のグラフを作りたい方は参考にしてくださればと思います.

test.png

コード.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

fig,axes = plt.subplots(2,2,figsize=(6,4),
                        sharex="col",
                        layout="constrained")
for ax in axes.ravel():
    ax.set(xscale="log",yscale="log")

N = 20
norm = mpl.colors.LogNorm(vmin=2**0,vmax=2**N)
cmap = plt.get_cmap("Blues")
x = np.logspace(-2,2,100)
for i in range(N):
    axes[0,0].plot(x,2**i*x,color=cmap(norm(2**i)))   
    axes[0,1].plot(x,3**i*x,color=cmap(norm(2**i)))   
    axes[1,0].plot(x,4**i*x,color=cmap(norm(2**i)))   
    axes[1,1].plot(x,5**i*x,color=cmap(norm(2**i)))   
fig.colorbar(mpl.cm.ScalarMappable(norm=norm,cmap=cmap),ax=axes.ravel().tolist(),aspect=25)
fig.supxlabel(r"first axis $x$ [unit]")
fig.supylabel(r"second axis $y$ [unit]")
plt.savefig("test.png")
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4