2
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 5 years have passed since last update.

matplotlibでカラーバーを表示させずにカラーレンジを設定する

Last updated at Posted at 2020-04-14

やりたいこと

matplotlibのquiverをつかって流速場(ベクトル場)を表示するときにベクトルの色を流速の大きさにしてカラー表示する。かつ、色のレンジを設定しつつもカラーバー自体は表示させない。

動機

なんでこんなことしたいかというと、時刻を変えた流速場を複数論文に載せたいのですが、スペースの都合上、カラーバーは1個だけ表示させればいい。すべての図でカラーバーも一緒に出して、後から加工してもいいです。けど面倒なので後加工を避けるために、カラーレンジは揃えつつ、カラーバー自体は表示させない図を作ろうとしました。

やり方

ポイントはNormalizeを使うこと。以下コード。

import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
import numpy as np

for i in range(len(timeList)):
    fig = plt.figure(figsize=(fig_width,fig_height))
    ax1 = fig.add_subplot(111)
    umag = np.sqrt(Ux[i]**2 + Uy[i]**2 + Uz[i]**2)
    image = ax1.quiver(y[::vec_skip], z[::vec_skip], Uy[i][::vec_skip], Uz[i][::vec_skip], 
                    umag[::vec_skip], width=0.002, headwidth=3, headlength=3, scale=5, 
                    cmap="jet", norm = Normalize(vmin=0,vmax=.7))

umagが速度の大きさで、ここのレンジを設定しようとしてます。norm = Normalize(vmin=0,vmax=.7)でカラーレンジを0から0.7に設定してます。

ちなみにカラーバー表示させて、カラーレンジ設定するなら以下のコードでOK。

import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
import numpy as np

for i in range(len(timeList)):
    fig = plt.figure(figsize=(fig_width,fig_height))
    ax1 = fig.add_subplot(111)
    umag = np.sqrt(Ux[i]**2 + Uy[i]**2 + Uz[i]**2)
    image = ax1.quiver(y[::vec_skip], z[::vec_skip], Uy[i][::vec_skip], Uz[i][::vec_skip], 
                    umag[::vec_skip], width=0.002, headwidth=3, headlength=3, scale=5, 
                    cmap="jet")
    cb = fig.colorbar(image, ax=ax1, label="Umag", )
    cb.mappable.set_clim(0.0,0.7)

最初この記事 https://qiita.com/kumamupooh/items/c793a6781a753eca6d8e
を参考にしてカラーバーのレンジを設定しようとしましたが、最近のmatplotlibだとmappable.set_climにしろって警告がでました。

Normalizeも使わず、colorbarも表示させないと、表示してるデータで自動にカラーレンジが設定されます。速度場のデータが変わるとレンジが変わるという悲しいことになります。

ちなみにmappable.set_climとNormalizeを同時に設定すると、mappable.set_climの設定が優先されて、Normalizeの設定は無視されます(無視されてるように見える)。

2
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
2
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?