3

I have historical baseball data that I'm trying to visualize in a simple matplotlib plot Within 1 subplot I want to have a table that shows average statistics over the past year, a line chart for each statistic, and then a final score which is calculated independently.

enter image description here

I know matplotlib has a table function so it would be simple to create a 5x3 table, but is it possible to insert plots as a value in a table? If not, are there any suggestions on what I should do? I guess I could create several subplots, but formatting would be wacky and not very dynamic. Appreciate the help.

Using @TheImportanceofBeingErnest's code, I'm encountering a bug with matplotlib where I can't see the x axis when I use gridspec:

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

Dick Thompson
  • 599
  • 1
  • 12
  • 26

1 Answers1

3

It is not possible to insert plots into a matplotlib table. However subplot grids allow to create a table-like behaviour.

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

To put several such plots into a figure you would approach this a bit differently and create a gridspec with several subgrids.

import matplotlib.pyplot as plt
from matplotlib import gridspec
import numpy as np


def summaryplot2subplot(fig, gs, data, col1, col2, finalsc):
    col2colors = ["g" if col2[i] > 1 else "r" for i in range(len(col2)) ]
    sgs = gridspec.GridSpecFromSubplotSpec(5,3, subplot_spec=gs, wspace=0.05, hspace=0,
                                           width_ratios=[0.9,0.7,2])
    axes = []
    for n in range(5):
        for m in range(3):
            axes.append(fig.add_subplot(sgs[n,m]))
    axes = np.array(axes).reshape(5,3)
    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(gs)
    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=11)
    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=col2colors[i], linewidth=1)


fig = plt.figure(figsize=(8,6))
gs = gridspec.GridSpec(2,2)


col1 = ["WAR", "ERA", "IP", "WHIP", "Final\nScore"]
finalsc = "D+"

for i in range(4):
    data = np.random.rand(100,4)
    col2 = np.random.rand(5)*2
    summaryplot2subplot(fig, gs[i], data, col1, col2, finalsc)

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • This is exactly what I need- thanks! I might be running an older version of matplotlib, but for some reason I had to insert a path for set_facecolor, ie `ax.patch.set_facecolor(col2colors[i])` – Dick Thompson Dec 13 '17 at 15:19
  • I'm trying to include this plot as a subplot of other plots in a 4x2 grid. Do you know if this is possible since we are already using subplots in this example? Appreciate the help – Dick Thompson Dec 19 '17 at 23:44
  • awesome- thanks so much! so if I just wanted to insert one of those into another plot (I basically just want to have one chart in a larger plot) how would I put all of that into one ax? would I have to use gridspec? – Dick Thompson Dec 20 '17 at 21:12
  • The larger plot would need to use gridspec. But it is only necessary for this table-like plot, all other subplots can potentially also be created differently. – ImportanceOfBeingErnest Dec 20 '17 at 21:56
  • so would it be possible to put all of this on one ax, where ax would be something like `ax = fig.add_subplot(4,2,1)` – Dick Thompson Dec 20 '17 at 22:04
  • No. The solution here uses different axes. So to put this plot into the `(421)` subplot's position use `gs = gridspec.GridSpec(4,2); summaryplot2subplot(fig, gs[0], data, col1, col2, finalsc)`. – ImportanceOfBeingErnest Dec 20 '17 at 22:07
  • sorry so how would I add that to my fig? It seems like I need to edit the overall fig to fit this in, right? because I basically replaced `ax1 = fig.add_subplot(4,2,1)` with what you wrote and I got an error with "unhashable type: slice". Maybe it would help if I showed my code? – Dick Thompson Dec 20 '17 at 22:22
  • what I'm asking to get is a way to basically add this to a plt subplot. So using this gridspec documentation how would I do that? https://matplotlib.org/users/gridspec.html because from what I understand you're using gs to add subplots. so then how would I append this function to another subplot with ax? – Dick Thompson Dec 20 '17 at 23:33
  • Not sure what you mean. The code from the answer creates 4 of those plots. If you only want a single one, call the function `summaryplot2subplot` only once (with the `gs[i]` of your choice) and produce the rest of the figure as you wish to. You do not "append this function to another subplot with ax" - whatever that means. – ImportanceOfBeingErnest Dec 20 '17 at 23:40
  • I managed to get it with the gridspec like you mentioned! Now I understand. I also have a question- maybe it's just a bug, but when I try to plot a chart next to this chart, the dates on the x axis won't show. I'll provide my code to show you what I mean- maybe there's something I'm missing appreciate the help – Dick Thompson Dec 22 '17 at 20:42
  • The code shown is not a [mcve]. Hence I cannot help. – ImportanceOfBeingErnest Dec 22 '17 at 20:51
  • Sorry am confused with what's wrong- I'm using your function, applying gridspecs, then plotting another chart next to it – Dick Thompson Dec 22 '17 at 20:54
  • I don't know what's wrong, but without a [mcve] I cannot help. – ImportanceOfBeingErnest Dec 22 '17 at 20:56
  • what isn't MCV? Just put the other function, and plug the rest in can you please be more specific – Dick Thompson Dec 22 '17 at 20:58
  • For some reason pandas turns the labels off. You can turn them on again : `plt.setp(ax2.get_xticklabels(), visible = True)`. – ImportanceOfBeingErnest Dec 22 '17 at 21:18
  • awesome, thanks so much- for future reference how did you figure that out? – Dick Thompson Dec 22 '17 at 21:21
  • I produced a [mcve] (that is what you should have done) and came to the conclusion that this has nothing to do with the use of the function from the answer. Then I commented out further lines from the code until I found that pandas apparently does not like twin axes. Then I googled a bit and found [this question](https://stackoverflow.com/questions/41042042/xaxis-tick-labels-have-disappeared-plotting-with-pandas-onto-subplot-but-are-st) about the same problem. – ImportanceOfBeingErnest Dec 22 '17 at 21:28
  • yeah that is really buggy, thanks for the insight. if I wanted to then reduce the tick frequency, would I have to convert the date columns to dates to get the min and max, and then convert those back to strings? – Dick Thompson Dec 22 '17 at 21:34
  • Hi, quick question- so I am doing the same methodology for a different dataframe with the same formatting. I'm just curious because I'm seeing 1e9 randomly being put in the middle of the chart and I have no idea why. That is probably a value in that chart, but I'm not sure why it's there since it's in a numpy array like everything else. Do you have a possible idea why this might be happening? Thanks – Dick Thompson Feb 06 '18 at 19:15
  • the type of the numpy array is float64 just like it would be with np.random.rand(100,4) – Dick Thompson Feb 06 '18 at 19:22
  • Who would have guessed that... You may add `ax.ticklabel_format(useOffset=False, style="plain")` in the loop. I updated the anwer. – ImportanceOfBeingErnest Feb 06 '18 at 21:57