3

I have added a table to the bottom of my plot, but there are a number of issues with it:

  1. The right has too much padding.
  2. The left has too little padding.
  3. The bottom has no padding.
  4. The cells are too small for the text within them.
  5. The table is too close to the bottom of the plot.
  6. The cells belonging to the row names are not colored to match those of the bars.

I'm going out of my mind fiddling with this. Can someone help me fix these issues?

Here is the code (Python 3):

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
# Set styles
plt.style.use(['seaborn-paper', 'seaborn-whitegrid'])
plt.style.use(['seaborn'])
sns.set(palette='colorblind')
matplotlib.rc("font", family="Times New Roman", size=12)

labels = ['n=1','n=2','n=3','n=4','n=5']
a = [98.8,98.8,98.8,98.8,98.8]
b = [98.6,97.8,97.0,96.2,95.4]
bar_width = 0.20
data = [a,b]
print(data)
colors = plt.cm.BuPu(np.linspace(0, 0.5, len(labels)))
columns = ('n=1', 'n=2', 'n=3', 'n=4', 'n=5')

index = np.arange(len(labels))
plt.bar(index, a, bar_width)
plt.bar(index+bar_width+.02, b, bar_width)
plt.table(cellText=data,
          rowLabels=['a', 'b'],
          rowColours=colors,
          colLabels=columns,
          loc='bottom')

plt.subplots_adjust(bottom=0.7)

plt.ylabel('Some y label which effect the bottom padding!')
plt.xticks([])
plt.title('Some title')
plt.show()

This is the output:

enter image description here

Update

This is working now, but in case someone else is having issues: Make sure you are not viewing your plots and the changes you make to them with IntelliJ SciView as it does not represent changes accurately and introduces some formatting issues!

pookie
  • 3,796
  • 6
  • 49
  • 105

1 Answers1

2

I think you can fix the first problem by setting the bounding box when you make the table using bbox like this:

bbox=[0, 0.225, 1, 0.2]

where the parameters are [left, bottom, width, height].

For the second issue (the coloring), that is because the color array is not corresponding to the seaborn coloring. You can query the seaborn color palette with

sns.color_palette(palette='colorblind')

this will give you a list of the colors seaborn is using.

Check the modifications below:

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
# Set styles
plt.style.use(['seaborn-paper', 'seaborn-whitegrid'])
plt.style.use(['seaborn'])
sns.set(palette='colorblind')
matplotlib.rc("font", family="Times New Roman", size=12)

labels = ['n=1','n=2','n=3','n=4','n=5']
a = [98.8,98.8,98.8,98.8,98.8]
b = [98.6,97.8,97.0,96.2,95.4]
bar_width = 0.20
data = [a,b]

colors = sns.color_palette(palette='colorblind')
columns = ('n=1', 'n=2', 'n=3', 'n=4', 'n=5')

index = np.arange(len(labels))
fig = plt.figure(figsize=(12,9))
plt.bar(index, a, bar_width)
plt.bar(index+bar_width+.02, b, bar_width)
plt.table(cellText=data,
          rowLabels=[' a ', ' b '],
          rowColours=colors,
          colLabels=columns,
          loc='bottom',
          bbox=[0, 0.225, 1, 0.2])

fig.subplots_adjust(bottom=0.1)

plt.ylabel('Some y label which effect the bottom padding!')
plt.xticks([])
plt.title('Some title')
plt.show()

I also changed the subplot adjustment to subplot_adjust(bottom=0.1) because it wasn't coming out right otherwise. Here is the output:

Output

William Miller
  • 9,839
  • 3
  • 25
  • 46
  • 1
    Thanks. I tried your modifications and while it does look nicer and you've solved some issues (thank you for that!), the table is now appearing more-or-less in the middle of the plot :( – pookie Jan 11 '19 at 08:46
  • Wow. After messing around for hours, I've managed to get it right. A large part of the issue I was having is that I was using IntelliJ's `SciView` to view the plots... which causes it to format incorrectly, so adjusting it was very difficult to do! Thanks again! – pookie Jan 11 '19 at 10:39