本文介绍了如何在matplotlib的绘图画布内放置可变长度的刻度标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转这个:类似于以下内容:

I want to turn this:into something resembling this:

我遇到的所有指令都只是对标签位置应用了一个偏移量,因此只有在标签的宽度已知时才有效.

All of the instructions I have come across just apply an offset to the label position and therefore only work if the labels are of a known width.

产生与示例相似的图的代码:

code to produce a similar plot than the example:

#!/usr/bin/env python3
import matplotlib.pyplot as plt
yvalues = [ 'short name', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', 'slightly longer name' ]
xvalues = [ [1, 5], [3, 7], [2, 6] ]
fig, ax = plt.subplots()
fig.set_size_inches(1280/fig.get_dpi(), 720/fig.get_dpi())
ax.axvline(40)
for i in range(len(yvalues)):
    ax.plot(xvalues[i], (yvalues[i], yvalues[i]))
plt.show()

推荐答案

将以下两行添加到您的代码中

Adding the following two lines to your code

ax.tick_params(axis="y", direction="in", pad=-10)
plt.setp(ax.get_yticklabels(),ha="left")

将导致以下情节

这篇关于如何在matplotlib的绘图画布内放置可变长度的刻度标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 07:43