Matplotlib xlabel间距

时间:2012-10-24 04:44:48

标签: python matplotlib

我已经在这个问题的底部包含了产生以下情节的所有代码:

errorbar with xlabel overlaps

正如你所看到的那样,xlabels(尽管被旋转w / fig.autofmt_xdate()重叠到了难以辨认的程度。

我的问题:是否有一种聪明的方法来实现这一点,以便这些标签半正确间隔?并非如此,我必须手动进入 - 这些情节必须用自动脚本制作。

import pylab as pl 

fig = pl.figure()
ax = fig.add_subplot(111)

vshiftDict = {'2.55958': 'FeII1608',
 '2.69745': 'AlII1670',
 '3.10440': 'AlIII1854',
 '3.12237': 'AlIII1862',
 '4.25461': 'FeII2374'}

qvalDict = {'AlII1670': {'e': 64.57000000000001,
              'l': 1670.78861,
              'q': 270.0,
              'v': -97.94999999999999,
              'w': 59851.97612760839,
              'x': -2704805.050627638},
 'AlIII1854': {'e': 353.45,
               'l': 1854.708966,
               'q': 458.0,
               'v': -316.38,
               'w': 53916.81489288709,
               'x': -5093214.27969269},
 'AlIII1862': {'e': 475.99,
               'l': 1862.780325,
               'q': 224.0,
               'v': 96.87,
               'w': 53683.19530645677,
               'x': -2501844.7657091334},
 'FeII1260': {'l': 1260.535572,
              'q': -1165.0,
              'w': 79331.35900428203,
              'x': 8805048.040363163},
 'FeII1608': {'e': 100.72,
              'l': 1608.450852,
              'q': -1165.0,
              'v': 87.42999999999999,
              'w': 62171.62300959135,
              'x': 11235293.423693288},
 'FeII1611': {'l': 1611.200369,
              'q': 1330.0,
              'w': 62065.526997157736,
              'x': -12848484.124150252},
 'FeII2249': {'l': 2249.875472,
              'q': 1604.0,
              'w': 44446.90439293788,
              'x': -21637821.99006887},
 'FeII2260': {'l': 2260.779108,
              'q': 1435.0,
              'w': 44232.538971250964,
              'x': -19451841.88995395},
 'FeII2344': {'l': 2344.212747,
              'q': 1375.0,
              'w': 42658.244277519065,
              'x': -19326375.791196708},
 'FeII2367': {'l': 2367.58924,
              'q': 1803.0,
              'w': 42237.05628937561,
              'x': -25594861.4444499},
 'FeII2374': {'e': 138.51,
              'l': 2374.460064,
              'q': 1625.0,
              'v': 73.96,
              'w': 42114.83760714032,
              'x': -23134969.61780541},
 'FeII2382': {'l': 2382.763995,
              'q': 1505.0,
              'w': 41968.06742499062,
              'x': -21501473.714337982},
 'FeII2586': {'l': 2586.649312,
              'q': 1515.0,
              'w': 38660.053195490924,
              'x': -23496376.043423213},
 'FeII2600': {'l': 2600.172114,
              'q': 1370.0,
              'w': 38458.99256498218,
              'x': -21358628.506247785},
 'MgI2026': {'l': 2026.4749788,
             'q': 87.0,
             'w': 49346.772620511765,
             'x': -1057088.1320477128},
 'MgI2852': {'l': 2852.962797,
             'q': 90.0,
             'w': 35051.28076158366,
             'x': -1539534.113091333},
 'MgII2796': {'l': 2796.353786,
              'q': 212.0,
              'w': 35760.854188283316,
              'x': -3554501.2857563947},
 'MgII2803': {'l': 2803.530982,
              'q': 121.0,
              'w': 35669.30440292883,
              'x': -2033955.4148985}}


transitions = []
xpositions = []
for transition in vshiftDict.itervalues():
  transitions.append(transition)
  xpositions.append( qvalDict[transition]['x'])
  pop = ax.errorbar(qvalDict[transition]['x'], qvalDict[transition]['v'], yerr=qvalDict[transition]['e'], fmt='bo')

ax.set_xlabel("x (-2cq/w)")
ax.set_ylabel("dv (m/s)")

ax.set_xticks( xpositions )
ax.set_xticklabels( transitions )

fig.autofmt_xdate()
fig.savefig('overlap.pdf')

1 个答案:

答案 0 :(得分:0)

这非常麻烦,但您可以按照以下步骤操作:Mathplotlib - automatically-make-room-for-tick-labels

tight_layout方法应该自动修复它,但它对你的情况不起作用。