【问题标题】:Matplotlib bar plot with table formatting带有表格格式的 Matplotlib 条形图
【发布时间】:2019-06-05 21:37:46
【问题描述】:

我在情节底部添加了一个表格,但它存在许多问题:

  1. 右侧的填充过多。
  2. 左侧的填充太少。
  3. 底部没有填充。
  4. 单元格太小,无法容纳其中的文本。
  5. 表格太靠近绘图底部。
  6. 属于行名的单元格的颜色与条形的颜色不匹配。

我正在发疯地摆弄这个。有人可以帮我解决这些问题吗?

这里是代码(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()

这是输出:

更新

这正在工作,但如果其他人遇到问题:请确保您没有查看您的绘图以及您使用 IntelliJ SciView 对它们所做的更改,因为它不能准确地表示更改并引入了一些格式问题!

【问题讨论】:

    标签: python-3.x matplotlib bar-chart


    【解决方案1】:

    我认为您可以通过在使用bbox 制作表格时设置边界框来解决第一个问题,如下所示:

    bbox=[0, 0.225, 1, 0.2]
    

    其中参数为[left, bottom, width, height]

    对于第二个问题(着色),这是因为color 数组与seaborn 着色不对应。您可以使用

    查询seaborn 调色板
    sns.color_palette(palette='colorblind')
    

    这将为您提供seaborn 正在使用的颜色列表。

    检查以下修改:

    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()
    

    我还将子图调整更改为subplot_adjust(bottom=0.1),因为否则它不会正确显示。这是输出:

    【讨论】:

    • 谢谢。我尝试了您的修改,虽然它看起来确实更好,并且您已经解决了一些问题(谢谢!),该表格现在或多或少地出现在情节中间:(
    • 哇。在搞砸了几个小时后,我设法把它弄对了。我遇到的很大一部分问题是我使用 IntelliJ 的SciView 来查看绘图......这导致它的格式不正确,因此很难调整它!再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多