Python 3.x 带表格格式的Matplotlib条形图
我在绘图的底部添加了一个表,但它存在一些问题:Python 3.x 带表格格式的Matplotlib条形图,python-3.x,matplotlib,bar-chart,Python 3.x,Matplotlib,Bar Chart,我在绘图的底部添加了一个表,但它存在一些问题: 右边的填充物太多 左边的填充物太少 底部没有填充物 单元格太小,无法容纳其中的文本 表格离绘图底部太近了 属于行名称的单元格的颜色与条形图的颜色不匹配 我把这个摆弄得快疯了。有人能帮我解决这些问题吗 下面是代码(Python 3): 这是输出: 更新 这项功能现在可以使用了,但如果其他人有问题:请确保您没有使用IntelliJ SciView查看绘图和对绘图所做的更改,因为它不能准确地表示更改,并且会导致一些格式问题 我认为您可以通过在使用bbo
这项功能现在可以使用了,但如果其他人有问题:请确保您没有使用
IntelliJ SciView
查看绘图和对绘图所做的更改,因为它不能准确地表示更改,并且会导致一些格式问题 我认为您可以通过在使用bbox
制作表格时设置边界框来解决第一个问题,如下所示:
bbox=[0, 0.225, 1, 0.2]
其中参数为[左、下、宽、高]
对于第二个问题(着色),这是因为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\u adjust(bottom=0.1)
,因为它在其他情况下不会正确显示。以下是输出:
谢谢。我试过你的修改,虽然它看起来更好,你已经解决了一些问题(谢谢你!),表现在或多或少出现在情节的中间:(哇。在胡闹了几个小时后,我终于把它弄对了。我遇到的一个很大的问题是,我使用IntelliJ的
SciView
来查看绘图…这导致它的格式不正确,所以调整它非常困难!再次感谢!
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()