具有表格格式的Matplotlib条形图 [英] Matplotlib bar plot with table formatting

查看:47
本文介绍了具有表格格式的Matplotlib条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在情节的底部添加了一个表格,但是有很多问题:

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

  1. 右边的填充物太多.
  2. 左边的填充物太少了.
  3. 底部没有填充物.
  4. 单元格对于其中的文本而言太小了.
  5. 表离图的底部太近了.
  6. 属于行名的单元格的颜色与栏的颜色不匹配.

我对此不屑一顾.有人可以帮我解决这些问题吗?

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

以下是代码(Python 3):

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

这是输出:

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

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!

推荐答案

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

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]

其中的参数为 [左侧,底部,宽度,高度] .

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

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

这将为您提供 seaborn 使用的颜色的列表.

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

检查以下修改:

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),因为否则它不会正确显示.这是输出:

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

这篇关于具有表格格式的Matplotlib条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆