【问题标题】:Adding one colorbar to multiple plots in one graph将一个颜色条添加到一张图中的多个图
【发布时间】:2020-07-23 03:22:48
【问题描述】:

我正在尝试将颜色条附加到我的 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)

【问题讨论】:

    标签: python python-3.x matplotlib colorbar


    【解决方案1】:

    这个图

    运行以下脚本生成

    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()
    

    【讨论】:

    • 非常感谢!它起作用了,它帮助我理解了添加颜色条的机制。
    猜你喜欢
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 2021-08-14
    • 2015-08-18
    相关资源
    最近更新 更多