HHa*_*ins 18 python matplotlib python-3.x
我正在尝试制作类似于这个excel示例的绘图:

我想知道是否在x刻度标签上有第二层(例如"5年统计摘要").我知道我可以使用多行刻度标签,\n但我希望能够独立地移动这两个级别.
beh*_*uri 18
这很接近:

fig = plt.figure( figsize=(8, 4 ) )
ax = fig.add_axes( [.05, .1, .9, .85 ] )
ax.set_yticks( np.linspace(0, 200, 11 ) )
xticks = [ 2, 3, 4, 6, 8, 10 ]
xticks_minor = [ 1, 5, 7, 9, 11 ]
xlbls = [ 'background', '5 year statistical summary', 'future build',
'maximum day', '90th percentile day', 'average day' ]
ax.set_xticks( xticks )
ax.set_xticks( xticks_minor, minor=True )
ax.set_xticklabels( xlbls )
ax.set_xlim( 1, 11 )
ax.grid( 'off', axis='x' )
ax.grid( 'off', axis='x', which='minor' )
# vertical alignment of xtick labels
va = [ 0, -.05, 0, -.05, -.05, -.05 ]
for t, y in zip( ax.get_xticklabels( ), va ):
t.set_y( y )
ax.tick_params( axis='x', which='minor', direction='out', length=30 )
ax.tick_params( axis='x', which='major', bottom='off', top='off' )
Run Code Online (Sandbox Code Playgroud)
由于缺乏声誉,我无法评论behzad的答案.我发现他的解决方案非常有用,但我想我会分享它而不是通过使用set_y()来控制垂直对齐,我只是添加了一个换行符来垂直偏移标签.所以,对于上面的例子:
xlbls = [ 'background', '\n5 year statistical summary', 'future build',
'\nmaximum day', '\n90th percentile day', '\naverage day' ]
Run Code Online (Sandbox Code Playgroud)
对我来说,这是一个更好的解决方案,可以保持多线标签和多层标签垂直对齐.
| 归档时间: |
|
| 查看次数: |
15852 次 |
| 最近记录: |