2017-11-25 124 views
0

我在同一圖中標註兩個不同的直方圖,如下所示。但是,這兩個直方圖的圖例以一個框的格式顯示。我嘗試過不同的方法來改變框,但都沒有工作。我不知道如何實現這樣的功能?到去這樣做enter image description here更改Python直方圖的圖例格式

+0

你用什麼工具來生成數字? –

回答

1

一種方式是明確指定傳說處理由手:你如何設置好一切,這可能看起來

handle1 = matplotlib.lines.Line2D([], [], c='r') 
handle2 = matplotlib.lines.Line2D([], [], c='b') 
plt.legend(handles=[handle1, handle2]) 

當然,根據一個很好的協議如下:

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.lines import Line2D 

# Generate some data that sort of looks like that in the question 
np.random.seed(0) 
x1 = np.random.normal(2, 1, 200) 
x2 = np.random.exponential(1, 200) 

# Plot the data as histograms that look like unfilled lineplots 
fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.hist(x1, label='target', histtype='step') 
ax.hist(x2, label='2nd halo', histtype='step') 

# Create new legend handles but use the colors from the existing ones 
handles, labels = ax.get_legend_handles_labels() 
new_handles = [Line2D([], [], c=h.get_edgecolor()) for h in handles] 

plt.legend(handles=new_handles, labels=labels) 
plt.show() 

enter image description here