我想为不同的模型绘制多条曲线。我有不同结构的曲线。结构需要具有相同的颜色,不同的模型需要不同的线型。然后我想要 2 个图例,一个具有与结构相对应的颜色,另一个具有线条样式。这是我已经拥有的代码:
谢谢!!!
plt.figure(figsize=(12,8))
styles = ['-', '--']
colors = ['blue', 'orange', 'green', 'red', 'purple', 'brown', 'pink', 'grey']
plans = ['Clinical', 'AP']
for j, plan in enumerate(plans):
for i, structure in enumerate(structures_dvh):
# for i, structure in enumerate(structure_test):
if plan == 'Clinical':
mean_dose, mean_volume = compute_mean_dvh(Lung_R2_adap, structure)
plt.plot(mean_dose, mean_volume, label = structure, c = colors[i], ls = styles[j])
elif plan == 'AP':
mean_dose, mean_volume = compute_mean_dvh(Lung_AP, structure)
plt.plot(mean_dose, mean_volume, c = colors[i], ls = styles[j])
elif plan == 'PS':
mean_dose, mean_volume = compute_mean_dvh(Lung_PS, structure)
plt.plot(mean_dose, mean_volume, c = colors[i], ls = styles[j])
# plt.legend(handles = linestyle_legend_handles, loc = 'lower left')
plt.legend(loc = 'lower center', bbox_to_anchor=(0.5, -0.25), fancybox=True, shadow=True, ncol=3)
plt.title('Mean DVH')
plt.xlabel('Relative dose')
plt.ylabel('Volume [%]')
plt.grid(True)
plt.tight_layout()
plt.show()
有多种方法可以解决这个问题(例如,参见matplotlib:同一图表上的2个不同图例和这个Matplotlib示例),但这里有一个基本示例:
from matplotlib import pyplot as plt
import numpy as np
plans = ["Clinical", "AP"]
structures = ["A", "B"]
colours = ["blue", "orange"]
styles = ["-", "--"]
# dictionary to store all your plotted lines
lines = {p: [] for p in plans}
fig, ax = plt.subplots()
N = 10
x = np.linspace(0, 1, N)
for j, plan in enumerate(plans):
for i, structure in enumerate(structures):
# plot some fake data
l, = ax.plot(x, np.random.randn(N) + j * 2 + i, color=colours[i], ls=styles[j])
# append the plotted lines
lines[plan].append(l)
locs = ["upper left", "upper right"]
# create legend for each plan (I've put the plan name as a title for the legend)
for i, plan in enumerate(plans):
leg = ax.legend(lines[plan], structures, title=plan, loc=locs[i])
ax.add_artist(leg)