python - matplotlib 中的多行 x 刻度标签

标签 python python-3.x matplotlib

我正在尝试制作类似于此 excel 示例的绘图:

example

我想知道在 x 刻度标签上是否还有第二层(例如“5 年统计摘要”)。我知道我可以使用 \n 制作多行刻度标签,但我希望能够独立移动两个级别。

最佳答案

这就接近了:

xtick

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' )

关于python - matplotlib 中的多行 x 刻度标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20532614/

相关文章:

Python:Pandas,处理间隔列名

当我在控制台之外执行时,Python 脚本执行目录发生变化

python - 如何将 matplotlib 图导出为带有可编辑文本字段的矢量图形?

python - 如何在Python中生成由多个图组成的动画?

python - 如何根据 ODR 结果计算标准误差?

python - 如何验证一个图在 networkx 中是否有交叉边?

python - 仅列出子进程中的文件和目录名称 ls -l

python - 如何在x轴上显示所有日期值?

python - 将位转换为字节序列

python - key 错误 : 0 ; Python removing columns from a list