我有一个Pandas数据帧,我想将其绘制为matplotlib表.到目前为止,我有一部分使用以下代码:
import numpy as np
randn = np.random.randn
from pandas import *
idx = Index(arange(1,11))
df = DataFrame(randn(10, 5), index=idx, columns=['A', 'B', 'C', 'D', 'E'])
vals = np.around(df.values,2)
fig = plt.figure(figsize=(15,8))
ax = fig.add_subplot(111, frameon=True, xticks=[], yticks=[])
the_table=plt.table(cellText=vals, rowLabels=df.index, colLabels=df.columns,
colWidths = [0.03]*vals.shape[1], loc='center')
table_props = the_table.properties()
table_cells = table_props['child_artists']
clm = cm.hot(vals)
for cell in table_cells:
cell.set_height(0.04)
# now i would like to set the backgroundcolor of the cell
Run Code Online (Sandbox Code Playgroud)
在这结束时,我想根据色彩图设置单元格的背景颜色 - 但是如何在没有索引的clm数组中查找它?
另一个问题:我可以以某种方式将格式字符串传递给表,以便将文本格式化为2位小数吗?
Andy,任何提示都表示赞赏
我正在寻找一种以表格格式(使用Python)绘制2d表格数据的方法,其中表格单元格像热图.像这样的东西:

我发现了很多使用matplotlib的常规热图的例子 - 但实际上并没有像这个链接那样产生输出的东西.任何提示赞赏.
我是Python的新手,我无法解决这个问题.我有以下功能定义:
def FlipCoins(num_flips):
heads_rounds_won = 0
for i in range(10000):
heads = 0
tails = 0
for j in range(num_flips):
dice = random.randint(0,1)
if dice==1: heads += 1
else: tails += 1
if heads > tails: heads_rounds_won += 1
return heads_rounds_won
Run Code Online (Sandbox Code Playgroud)
它应该做什么(但显然不是):翻转硬币num_flip次数,计算头部和尾部,看看是否有更多的头部而不是尾部.如果是,则递增head_rounds_won1.重复10000次.
我认为这head_rounds_won将约为5000(50%).并且它将奇数作为输入.例如,3,5或7将产生约50%.然而,偶数会产生更低的结果,更像是34%.特别是小数字,偶数更高,例如800,差异达到50%要窄得多.
为什么会这样?不应该有任何输入产生约50%的头/尾?