ratio.set_index(['promo_name', 'model'], inplace=True)
ratio = ratio.reindex(index = [('A', 'plan'), ('A', 'predict'),
('B', 'plan'), ('B', 'predict'),
('C', 'plan'), ('C', 'predict')])
plt.rcParams["figure.figsize"] = (12,5)
ratio.plot(kind='bar', stacked=True)
plt.xticks(rotation=30)
plt.show()
你好我有一个数据表,如上表所示,我想用值绘制一个堆积的条形图,例如:
但是到目前为止,它只能这样工作,我的代码如下:
RunAsync()
答案 0 :(得分:1)
以下是使用matplotlib的示例。
准备源数据:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
source = {'promo_name': {0: 'A', 1: 'B', 2: 'C', 3: 'A', 4: 'B', 5: 'C'},
'in': {0: 0.87, 1: 0.77, 2: 0.54, 3: 0.59, 4: 0.5, 5: 0.46},
'out': {0: 0.13, 1: 0.23, 2: 0.46, 3: 0.41, 4: 0.5, 5: 0.54},
'model': {0: 'plan', 1: 'plan',2: 'plan', 3: 'predict', 4: 'predict', 5: 'predict'}}
ratio = pd.DataFrame.from_dict(source)
ratio.head()
Out[1]:
promo_name in out model
0 A 0.87 0.13 plan
1 B 0.77 0.23 plan
2 C 0.54 0.46 plan
3 A 0.59 0.41 predict
4 B 0.50 0.50 predict
5 C 0.46 0.54 predict
这是一个解决方案:
ind = list(ratio.index)
plt.rcParams["figure.figsize"] = (12,5)
bars_b = plt.bar(list(range(len(ind))), ratio['in'], width=0.3, color = 'b', label = 'in')
bars_r = plt.bar(list(range(len(ind))), ratio['out'], bottom = ratio['in'], width=0.3, color = 'r', label = 'out')
plt.xticks(rotation=30)
plt.legend()
for i in range(len(bars_b)):
bar_b = bars_b[i]
bar_r = bars_r[i]
plt.gca().text(bar_b.get_x()+ bar_b.get_width()/2,
bar_b.get_height()/2,
ratio['in'][i],
ha='center',
color = 'w')
plt.gca().text(bar_r.get_x()+ bar_r.get_width()/2,
bar_r.get_height()/2 + bar_b.get_height(),ratio['out'][i], ha='center', color = 'w')
plt.show()
此处结果:
答案 1 :(得分:1)
使用matplotlib,您可以轻松地了解这些条的位置和宽度:
if(seattxt.length() == 0) // or if(seattxt.equals(""))
{
Toast.makeText(Seatinglayout.this, "Select a seat to continue", Toast.LENGTH_LONG).show();
}
else
{
// start Passengerinformation Activity.
}
并添加文本标签(您可以在该位置玩耍):
import matplotlib.pyplot as plt
# plan, predict
Aout = (0.87, 0.59)
Ain = (0.13, 0.41)
Bout = (0.77, 0.50)
Bin = (0.23, 0.50)
Cout = (0.54, 0.46)
Cin = (0.46, 0.54)
width = 1.0 # the width of the bars
A_positions = [0, 1] # Positions for A bars
p1A = plt.bar([0, 1], (1.0, 1.0), width, color='g', label='out')
p2A = plt.bar([0, 1], Ain, width, color='b', label='in')
B_positions = [3, 4] # Positions for B bars
p1B = plt.bar(B_positions, (1.0, 1.0), width, color='g')
p2B = plt.bar(B_positions, Bin, width, color='b')
C_positions = [6, 7] # Positions for C bars
p1C = plt.bar(C_positions, (1.0, 1.0), width, color='g')
p2C = plt.bar(C_positions, Cin, width, color='b')
positions = A_positions + B_positions + C_positions # All together for ticks
plt.xticks(positions, ('A (plan)', 'A (predict)', 'B (plan)', 'B (predict)', 'C (plan)', 'C (predict)'))
plt.xticks(rotation=70)
plt.yticks([1, 0])
plt.legend()
plt.tight_layout()