Matplotlib bar plot with table formatting

佐手、 提交于 2020-05-27 14:48:54

问题


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:

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!


回答1:


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:



来源:https://stackoverflow.com/questions/54138482/matplotlib-bar-plot-with-table-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!