在一个图形中将一个颜色条添加到多个图

时间:2020-04-10 11:22:10

标签: python python-3.x matplotlib colorbar

我试图将颜色条附加到我的MatplotLib图上,该图在一个图形中绘制多个图(我不是在单个色条上寻找多个子图)。

在我的脚本中,我加载文件并绘制变量运行图,但是我想为第三个变量着色它们。

我找到了一种方法,但是它会在每个图上绘制颜色条,看起来像:1

我希望它看起来像:2,除了每个路径都应该着色。

这是我生成绘图的代码块:

import os
import glob
import mesa_reader as mesa
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Rectangle


fig, ax = plt.subplots(1, 1, sharex=True, sharey=True, figsize=(10,5), dpi=100)


counter = 0
for fname in glob.glob('LOGS_P_*'):
   a = mesa.MesaData(fname+'/LOGS1/history.data')

   counter = counter + 1
   if counter == 1:
      plt.plot(a.log_Teff, a.log_L, color='black', linestyle='solid', linewidth=0.8)

      points = np.array([a.log_Teff, a.log_L]).T.reshape(-1, 1, 2)
      segments = np.concatenate([points[:-1], points[1:]], axis=1)

      # Create a continuous norm to map from data points to colors
      norm = plt.Normalize(-20, a.lg_mtransfer_rate.max())
      lc = LineCollection(segments, cmap='viridis', norm=norm)

      # Set the values used for colormapping
      lc.set_array(a.lg_mtransfer_rate)
      lc.set_linewidth(2)
      fig.colorbar(ax.add_collection(lc), ax=ax)

   else:
      plt.plot(a.log_Teff, a.log_L, color='black', linestyle='solid', linewidth=0.8)

      points = np.array([a.log_Teff, a.log_L]).T.reshape(-1, 1, 2)
      segments = np.concatenate([points[:-1], points[1:]], axis=1)

      # Create a continuous norm to map from data points to colors
      norm = plt.Normalize(-20, a.lg_mtransfer_rate.max())
      lc = LineCollection(segments, cmap='viridis', norm=norm)

      # Set the values used for colormapping
      lc.set_array(a.lg_mtransfer_rate)
      lc.set_linewidth(2)
      fig.colorbar(ax.add_collection(lc), ax=ax)

1 个答案:

答案 0 :(得分:0)

这个数字

a sine and a cosine

运行以下脚本产生

from numpy import array, concatenate, linspace, cos, pi, sin
import matplotlib.pyplot as plt

from matplotlib.collections import LineCollection
from matplotlib.colors import Normalize
from matplotlib.cm import ScalarMappable

def segments_from(x, y):
    tmp = array((x, y)).T.reshape(-1,1,2)
    return concatenate([tmp[:-1], tmp[1:]], axis=1)

t = linspace(0, 3, 301)
w1, w2 = 2*pi, 3*pi
s1, s2 = sin(w1*t), sin(w2*t)
c1, c2 = cos(w1*t), cos(w2*t)

norm = Normalize(-2, +2)
cmap = plt.get_cmap('inferno')

fig, ax = plt.subplots()
ax.set_xlim(0, 3)
ax.set_ylim(-2, 2)

for y, v in ((1.6*c1, c2), (0.9*s1, s2)):
    lc = LineCollection(segments_from(t, y),
                        linewidths=4,
                        norm=norm, cmap=cmap)
    lc.set_array(v)
    ax.add_collection(lc)

fig.colorbar(ScalarMappable(norm=norm, cmap=cmap))

plt.show()