【发布时间】: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