我试图让条形图的python表格对齐。 例如,在附图中,您将看到x轴未正确对齐到python表所关闭的垂直线。
我尝试修改图的比例
我希望表格的字体大小为40,以便在我打印IEEEtran纸张时可见。
#!/usr/bin/env python
import csv
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
def plot_bar(dataset):
matplotlib.rc('font', family='sans-serif')
matplotlib.rc('font', serif='Helvetica Neue')
matplotlib.rc('text', usetex='false')
matplotlib.rcParams.update({'font.size': 30})
fig = plt.figure()
ax = fig.add_subplot(111)
fig = matplotlib.pyplot.gcf()
fig.set_size_inches(30.0,7.5)
N = len(dataset[1])
Load = dataset[0]
QoS = dataset[1]
Energy = dataset[2]
ind = np.arange(N)
width = 0.35
plt.tick_params(axis='both', which='major', labelsize=35, pad=15)
plt.tick_params(axis='y', which='minor', labelsize=35, pad=15)
rects1 = ax.bar(ind, QoS, width,
color='0.2',
label='HP')
rects3 = ax.bar(ind+width, Energy, width,
color='0.4',
label='OM')
lns = [rects1, rects3]
labs = [l.get_label() for l in lns]
ax.legend(lns, labs, ncol=2)
ax.set_xlim(-width,len(ind)+width)
ax.set_ylim(0, 16000)
ax.set_ylabel('RPS/Watt', fontsize=35)
ax.set_xlabel('Percentage of Max Capacity', fontsize=35)
xTickMarks = dataset[0]
ax.set_xticks(ind+width)
xtickNames = ax.set_xticklabels(xTickMarks)
plt.setp(xtickNames, rotation=0, fontsize=40)
plt.xticks([])
ax.yaxis.grid()
cell_text = [['2S-0.65GHz', '3S-0.65GHz', '4S-0.65GHz', '4S-0.65GHz', '2B2S-1.15GHz', '2B2S-1.15GHz', '3B2S-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz','2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz'],
['2S-0.65GHz', '3S-0.65GHz', '4S-0.65GHz', '4S-0.65GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz']]
colors=['0.2','0.4']
rows = ['HP','OM']
Loc='right'
the_table = plt.table(cellText=cell_text,
rowLabels=rows,
colLabels=Load,
rowColours=colors,
cellLoc='right',
loc='bottom')
the_table.scale(1,2.5)
the_table.auto_set_font_size(False)
the_table.set_fontsize(12)
plt.subplots_adjust(left=0.2, bottom=0.2)
ax.xaxis.labelpad = 70
ax.yaxis.labelpad = 20
fig.savefig('rps-watt' +'.eps',format='eps',bbox_inches='tight', pad_inches=0.1, dpi=1000)
dataset = [['29%', '40%', '51%', '63%', '69%', '71%', '74%', '77%', '80%', '83%', '86%', '89%', '91%', '94%', '97%', '100%'], [6524.0, 8749.0, 10470.0, 13096.0, 13126.0, 12965.0, 13493.0, 13717.0, 14351.0, 14993.0, 15308.0, 14320.0, 13179.0, 9809.0, 10168.0, 10621.0], [6524.0, 8749.0, 10470.0, 13096.0, 6827.0, 5586.0, 7697.0, 8205.0, 8298.0, 8733.0, 8887.0, 9278.0, 9659.0, 9809.0, 10168.0, 10621.0]]
plot_bar(dataset)
答案 0 :(得分:6)
我认为这就是你要找的东西
-added参数" spare_width"这使条形图正确。
-resized图例字体和xlabel间距看起来更好,而不是阻止数据
- 添加" ggplot"风格 - 我更喜欢它。
- 通过将频率(GHz)移动到它自己的线路并通过bbox设置高度,将字体大小增加到35。
import csv
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import matplotlib
def plot_bar(dataset):
matplotlib.rc('font', family='sans-serif')
matplotlib.rc('font', serif='Helvetica Neue')
matplotlib.rc('text', usetex='false')
matplotlib.rcParams.update({'font.size': 30})
fig = plt.figure()
ax = fig.add_subplot(111)
fig = matplotlib.pyplot.gcf()
fig.set_size_inches(30.0,7.5)
N = len(dataset[1])
Load = dataset[0]
QoS = dataset[1]
Energy = dataset[2]
ind = np.arange(N)
width = 0.35
spare_width = (1 - width*2)/2
plt.tick_params(axis='both', which='major', labelsize=35, pad=15)
plt.tick_params(axis='y', which='minor', labelsize=35, pad=15)
rects1 = ax.bar(ind, QoS, width,
color='0.2',
label='HP')
rects3 = ax.bar(ind+width, Energy, width,
color='0.4',
label='OM')
lns = [rects1, rects3]
labs = [l.get_label() for l in lns]
ax.legend(lns, labs, ncol=2, fontsize=30,framealpha=0)
ax.set_xlim(-spare_width,len(ind)-spare_width)
ax.set_ylim(0, 16000)
ax.set_ylabel('RPS/Watt', fontsize=35)
ax.set_xlabel('Percentage of Max Capacity', fontsize=35)
xTickMarks = dataset[0]
ax.set_xticks(ind+width)
xtickNames = ax.set_xticklabels(xTickMarks)
plt.setp(xtickNames, rotation=0, fontsize=40)
plt.xticks([])
ax.yaxis.grid()
cell_text = [['2S\n0.65', '3S\n0.65', '4S\n0.65', '4S\n0.65',
'2B2S\n1.15','2B2S\n1.15', '3B2S\n1.15', '2S\n1.15',
'2S\n1.15', '2S\n1.15', '2S\n1.15','2S\n1.15',
'2S\n1.15', '2S\n1.15', '2S\n1.15', '2S\n1.15'],
['2S\n0.65', '3S\n0.65', '4S\n0.65', '4S\n0.65',
'2S\n1.15', '2S\n1.15', '2S\n1.15', '2S\n1.15',
'2S\n1.15', '2S\n1.15', '2S\n1.15', '2S\n1.15',
'2S\n1.15', '2S\n1.15', '2S\n1.15', '2B\n1.15']]
colors=['0.2','0.4']
rows = ['HP\nGHz','OM\nGHz']
Loc='right'
the_table = plt.table(cellText=cell_text,
rowLabels=rows,
colLabels=Load,
rowColours=colors,
cellLoc='center',
loc='bottom',
bbox=[0,-0.65,1,0.65])#x,y,w,h
the_table.scale(1,2.5)
the_table.auto_set_font_size(False)
the_table.set_fontsize(35)
plt.subplots_adjust(left=0.2, bottom=0.2)
ax.xaxis.labelpad = 260
ax.yaxis.labelpad = 20
fig.savefig('rps-watt' +'.eps',format='eps',bbox_inches='tight', pad_inches=0.1, dpi=1000)
dataset = [['29%', '40%', '51%', '63%', '69%', '71%', '74%', '77%', '80%', '83%', '86%', '89%', '91%', '94%', '97%', '100%'], [6524.0, 8749.0, 10470.0, 13096.0, 13126.0, 12965.0, 13493.0, 13717.0, 14351.0, 14993.0, 15308.0, 14320.0, 13179.0, 9809.0, 10168.0, 10621.0], [6524.0, 8749.0, 10470.0, 13096.0, 6827.0, 5586.0, 7697.0, 8205.0, 8298.0, 8733.0, 8887.0, 9278.0, 9659.0, 9809.0, 10168.0, 10621.0]]
plot_bar(dataset)
使用WinPython-64bit-3.4.4.1Qt5 download here创建此绘图