Matplotlib在单元格中创建带有线条图的表格?

2024-09-30 03:21:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有历史上的棒球数据,我正试图在一个简单的matplotlib图中可视化,我想有一个表,显示过去一年的平均统计数据,每个统计的折线图,然后是一个独立计算的最终得分。在

enter image description here

在一个表中插入一个5xlib函数,这是可能的吗?如果没有,有什么关于我该怎么做的建议吗?我想我可以创建几个子窗口,但是格式化会很奇怪,而且不是很动态。感谢你的帮助。在

使用@TheImportanceofBeingErnest的代码,我在matplotlib中遇到了一个bug,在使用gridspec时看不到x轴:

fig = plt.figure(figsize=(8.5,11))

gs_row1left = gridspec.GridSpec(1,1)
gs_row1right = gridspec.GridSpec(1,1)

summaryplot2subplot(fig, gs_row1left[0], data, col1, col2, finalsc)
ax.axis('off')

ax2 = fig.add_subplot(gs_row1right[0, 0])
df = pd.DataFrame({'year':['2001-01','2002-01','2003-01','2004-01','2005-01'], 'value':[100,200,300,400,500]})

barax = ax2.twinx()
df['value1']= df['value']*0.4

df['value2'] = df['value']*0.6# Let them be strings!



df.plot(x = ['year'], y = ['value'], kind = 'line', ax = ax2)

df.plot(x = ['year'], y= ['value1','value2'], kind = 'bar', ax = barax)



gs_row1left.update(left = 0.05, right = 0.48)
gs_row1right.update(left = 0.55, right = 0.98)
plt.show()

enter image description here


Tags: gsdfmatplotlibvaluefigpltaxyear
1条回答
网友
1楼 · 发布于 2024-09-30 03:21:57

无法将绘图插入matplotlib表中。然而,子块网格允许创建类似表的行为。在

import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(100,4)
col1 = ["WAR", "ERA", "IP", "WHIP", "Final\nScore"]
col2 = [0.23,1.60,0.28,0.02,0.38]
col2colors = ["red", "g", "r", "r", "r"]
finalsc = "D+"

fig, axes = plt.subplots(ncols=3, nrows=5, figsize=(4,2.6),
                         gridspec_kw={"width_ratios":[1,0.5,2]})
fig.subplots_adjust(0.05,0.05,0.95,0.95, wspace=0.05, hspace=0)

for ax in axes.flatten():
    ax.tick_params(labelbottom=0, labelleft=0, bottom=0, top=0, left=0, right=0)
    ax.ticklabel_format(useOffset=False, style="plain")
    for _,s in ax.spines.items():
        s.set_visible(False)
border = fig.add_subplot(111)
border.tick_params(labelbottom=0, labelleft=0, bottom=0, top=0, left=0, right=0)
border.set_facecolor("None")

text_kw = dict(ha="center", va="bottom", size=13)
for i,ax in enumerate(axes[:,0]):
    ax.text(0.5, 0.05, col1[i], transform=ax.transAxes, **text_kw)
for i,ax in enumerate(axes[:,1]):
    ax.text(0.5, 0.05, "{:.2f}".format(col2[i]),transform=ax.transAxes, **text_kw)
    ax.set_facecolor(col2colors[i])
    ax.patch.set_color(col2colors[i])
axes[-1,-1].text(0.5, 0.05, finalsc,transform=axes[-1,-1].transAxes, **text_kw)

for i,ax in enumerate(axes[:-1,2]):
    ax.plot(data[:,i], color="green", linewidth=1)


plt.show()

enter image description here

要将多个这样的图放入一个图中,您可以使用一些不同的方法来创建一个包含多个子网格的gridspec。

和13;
和13;

enter image description here

相关问题 更多 >

    热门问题