如何在图表上显示数据框中每列的最高百分比

时间:2018-10-08 07:54:00

标签: python

我的航班数据集包含

  

3列(“ on_time”,“ early”,“ delayed”)

     

12行(月)

为了说明每个月的出发时间,我在上面做了放样图。
 我的问题是,如何在图上显示各列(“ on_time”,“ early”,“ delayed”)相对于该月总飞行次数的最高百分比。

例如:

  

按时:8月份航班的10%
  早期:十月份的航班有70%

     

延误:6月80%的航班

 <div class="progress" style="height:20px; border-radius: 0; background-color: #ffaab2;">
      <div class="progress-bar" style="width:80%;height:20px; background-color: #ff5262">
      </div>
    </div>  

1 个答案:

答案 0 :(得分:0)

您可以在条形图中添加表格以显示数据并突出显示特定数据。

例如(受到table的启发)

import numpy as np
import matplotlib.pyplot as plt


data = np.random.rand(3,12)
for i in range(0,12):
    data[:,i]=data[:,i]/sum(data[:,i])*100.0

columns = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Nov', 'Oct', 'Dec')
rows = ['on_time', 'early', 'delayed']

values = np.arange(0, 110, 10)
value_increment = 1

colors = np.zeros((3,3))
colors[0,0]=0.5
colors[1,1]=0.5
colors[2,2]=0.5
n_rows = len(data)
n_columns = len(data[0])

index = np.arange(len(columns)) + 0.3
bar_width = 0.4

# Initialize the vertical-offset for the stacked bar chart.
y_offset = np.zeros(len(columns))

# Plot bars and create text labels for the table
cell_text = []
colors_cell = np.ones((3,12,3))
p=list()
for row in range(n_rows):
    p.append(plt.bar(index, data[row,:], bar_width, bottom=y_offset, color=colors[row]))
    #
    y_offset = y_offset + data[row,:]
    cell_text.append(['%1.1f' % (x) for x in data[row]])
plt.legend((p[0][0], p[1][0], p[2][0]), ('on_time', 'early', 'delayed'))
for column in range(n_columns):
    arg_i = np.argmax(data[:,column])
    colors_cell[2-arg_i, column, 0] = 0.5
    colors_cell[2-arg_i, column, 1] = 0.5
    colors_cell[2-arg_i, column, 2] = 0.5
# Reverse colors and text labels to display the last value at the top.
colors = colors[::-1]
cell_text.reverse()

# Add a table at the bottom of the axes
the_table = plt.table(cellText=cell_text,
                      cellColours=colors_cell,
                      rowLabels=rows,
                      rowColours=colors,
                      colLabels=columns,
                      loc='bottom')
# Adjust layout to make room for the table:
plt.subplots_adjust(left=0.2, bottom=0.2)

plt.yticks(values * value_increment, ['%d' % val for val in values])
plt.xticks([])

plt.show()

enter image description here