0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[matplotlib] contourfでカラーバーの上限と下限を設定する

Posted at

引っかかったところ

matplotlibのcontourf使用時にset_clim(vmin, vmax)で上限と下限を設定するとカラーバーがサチる.

例によってテイラー・グリーン渦の渦度を可視化してみる.
$$
\begin{aligned}
\omega &= 2 \sin x \sin y.
\end{aligned}
$$

import numpy as np
from numpy import pi, sin
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size

# Parameters
N = 100
lvls = 100
xmax = 2*pi
ymax = 2*pi
vmin = -1.2
vmax = 1.2

# Taylor-Green Vortices
x = np.linspace(0, xmax, N)
y = np.linspace(0, ymax, N)
X, Y = np.meshgrid(x, y)
omega = 2*sin(X)*sin(Y)

# PLOT
fig, ax = plt.subplots(1,2)
plt1 = ax[0].contourf(X, Y, omega, 100, cmap='RdBu_r')
fig.colorbar(plt1, ax=ax[0])

plt2 = ax[1].contourf(X, Y, omega, 100, cmap='RdBu_r')
plt2.set_clim(vmin=vmin, vmax=vmax)
fig.colorbar(plt2, ax=ax[1])
plt.show()

TG_Colourbar_sat.png

解決策

levels = (vmin, vmax, 100)で上限下限を指定してextednd = 'both'をオプションに加えるだけだった...

plt.contourf(X, Y, omega, np.linspace(vmin, vmax, 100), cmap='RdBu_r', extend='both')
plt.colorbar(ticks=np.linspace(vmin, vmax, 5))
plt.xticks([])
plt.yticks([])
plt.show()

TG_Correct.png

参考にしたサイト

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?