熊猫:Y轴在图表中的一致性

时间:2015-08-01 10:41:34

标签: python python-3.x pandas matplotlib ipython-notebook

.directive('shouldFocus', function(){
  return {
   restrict: 'A',
   link: function(scope,element,attrs){
     scope.$watch(attrs.shouldFocus,function(newVal,oldVal){
       element[0].scrollIntoView(false);
     });
   }
 };
})

有人告诉我,我可以使用rcParams.update({'figure.autolayout': True}) plt.figure(figsize=(14,9), dpi=600) reso_names = [x[0] for x in resolution3] reso_values = [x[1] for x in resolution3] plt.bar(range(len(reso_values[60:130])), reso_values[60:130], align='center') plt.xticks(range(len(reso_names[60:130])), list(reso_names[60:130]), rotation='vertical') plt.margins(0.075) plt.xlabel('Resolution Category Tier 3') plt.ylabel('Volume') plt.title('Resolution Category Tier 3 Volume', {'family' : 'Arial Black', 'weight' : 'bold', 'size' : 22}) plt.savefig('Reso3_A.pdf', format='pdf') plt.show() rcParams.update({'figure.autolayout': True}) plt.figure(figsize=(14,9), dpi=600) plt.bar(range(len(reso_values[0:60])), reso_values[0:60], align='center') plt.xticks(range(len(reso_names[0:60])), list(reso_names[0:60]), rotation='vertical') plt.margins(0.075) plt.xlabel('Resolution Category Tier 3') plt.ylabel('Volume') plt.title('Resolution Category Tier 3 Volume', {'family' : 'Arial Black', 'weight' : 'bold', 'size' : 22}) plt.savefig('Reso3_B.pdf', format='pdf') plt.show() 在这两个图表中实现相同的y轴,但我不确定在何处进行更改。

1 个答案:

答案 0 :(得分:1)

您可以明确设置y限制

plt.ylim(min(reso_values), max(reso_values)+1)
每次致电plt.savefig之前

要使用sharey,您需要创建包含两个(或更多)轴的单个数字:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import string

np.random.seed(2015)
N = 130
reso_values = np.random.randint(10, size=N)
reso_names = np.random.choice(list(string.letters), N*4).view('|S{}'.format(4))

fig, axs = plt.subplots(nrows=2, sharey=True, figsize=(14,18), dpi=50)

mpl.rcParams.update({'figure.autolayout': True})
fig.suptitle('Resolution Category Tier 3 Volume', 
          **{'family': 'Arial Black', 'size': 22, 'weight': 'bold'})

for ax, start, end in zip(axs, (0, 60), (60, N)):
    names, values = list(reso_names[start:end]), reso_values[start:end]
    ax.bar(range(len(values)), values, align='center')
    ax.set_xlabel('Resolution Category Tier 3')
    ax.set_ylabel('Volume')
    ax.set_xticks(range(len(names)))
    ax.set_xticklabels(names, rotation='vertical')
    ax.set_xlim(0, len(names))
# plt.savefig('Reso3.pdf', format='pdf')
fig.subplots_adjust(bottom=0.05, top=0.95)
plt.show()

enter image description here