我使用以下代码绘制条形图。需要以相反的顺序呈现图例。我该怎么办?
colorsArr = plt.cm.BuPu(np.linspace(0, 0.5, len(C2)))
p = numpy.empty(len(C2), dtype=object)
plt.figure(figsize=(11,11))
prevBar = 0
for index in range(len(C2)):
plt.bar(ind, C2[index], width, bottom=prevBar, color=colorsArr[index],
label=C0[index])
prevBar = prevBar + C2[index]
# positions of the x-axis ticks (center of the bars as bar labels)
tick_pos = [i+(width/2) for i in ind]
plt.ylabel('Home Category')
plt.title('Affinity - Retail Details(Home category)')
# set the x ticks with names
plt.xticks(tick_pos, C1)
plt.yticks(np.arange(0,70000,3000))
plt.legend(title="Line", loc='upper left' )
# Set a buffer around the edge
plt.xlim(-width*2, width*2)
plt.show()
答案 0 :(得分:33)
你可以打电话
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[::-1], labels[::-1], title='Line', loc='upper left')
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2016)
C0 = list('ABCDEF')
C2 = np.random.randint(20000, size=(len(C0), 3))
width = 1.0
C1 = ['foo', 'bar', 'baz']
ind = np.linspace(-width, width, len(C1))
colorsArr = plt.cm.BuPu(np.linspace(0, 0.5, len(C2)))
fig = plt.figure(figsize=(11,11))
ax = fig.add_subplot(1, 1, 1)
prevBar = 0
for height, color, label in zip(C2, colorsArr, C0):
h = ax.bar(ind, height, width, bottom=prevBar, color=color, label=label)
prevBar = prevBar + height
plt.ylabel('Home Category')
plt.title('Affinity - Retail Details(Home category)')
# positions of the x-axis ticks (center of the bars as bar labels)
tick_pos = [i+(width/2.0) for i in ind]
# set the x ticks with names
plt.xticks(tick_pos, C1)
plt.yticks(np.arange(0,70000,3000))
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[::-1], labels[::-1], title='Line', loc='upper left')
plt.show()
答案 1 :(得分:3)
我没有对此进行测试,因为我没有您的数据,但这是基于controlling legend entries上的文档。
handles = []
for index in range(len(C2)):
h = plt.bar(ind, C2[index], width, bottom=prevBar, color=colorsArr[index], label=C0[index])
handles.append(h)
prevBar = prevBar + C2[index]
plt.legend(title="Line", loc='upper left', handles=handles[::-1])
答案 2 :(得分:3)
或者您可以使用更简单的
handles, labels = ax.get_legend_handles_labels()
ax.legend(reversed(handles), reversed(labels), title='Line', loc='upper left')
答案 3 :(得分:1)
Use a negative number for the legend vertical spacing, like this:
declare @table table (ID int, StartDate date, EndDate date)
Insert Into @table
(
ID
, StartDate
, EndDate
)
Values
(1, '2017-01-01', '2017-02-01'),
(1, '2017-01-09', '2017-01-28'),
(1, '2017-04-01', '2017-04-30'),
(1, '2017-04-05', '2017-05-20'),
(1, '2017-04-20', '2017-06-12'),
(2, '2017-06-02', '2017-06-20'),
(2, '2017-06-14', '2017-07-31'),
(2, '2017-06-14', '2017-07-31'),
(2, '2017-06-19', '2017-07-31'),
(2, '2017-06-19', '2017-07-31')
;with cte as(
select
t1.ID
,t1.StartDate
,t1.EndDate
,DT = (select min(StartDate) from @table t2 where t2.StartDate > DATEADD(day,30,t1.StartDate))
from
@table t1),
cte2 as(
select
ID
,StartDate
,EndDate
,dense_rank() over (order by isnull(DT,(select max(StartDate) from cte))) as Flag
from
cte)
select
ID
,StartDate
,EndDate
,case when Flag % 2 = 0 then 2 else Flag % 2 end as Flag
from cte2