简体   繁体   English

matplotlib 3D 绘图的刻度标签位置

[英]tick label positions for matplotlib 3D plot

I am trying to work out how to set/correct the position of tick labels for a 3D matplotlib plot.我正在尝试解决如何设置/更正 3D matplotlib 图的刻度标签位置。 Tick labels do not align with the ticks.刻度标签与刻度不对齐。 The issue seems to be especially prominent when many tick labels are required.当需要许多刻度标签时,这个问题似乎特别突出。

I have modified an example ( http://matplotlib.org/examples/mplot3d/polys3d_demo.html ) from the matplotlib documentation to illustrate my question.我修改了 matplotlib 文档中的一个示例( http://matplotlib.org/examples/mplot3d/polys3d_demo.html )来说明我的问题。

from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
from matplotlib.colors import colorConverter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(10,10))
ax = fig.gca(projection='3d')

cc = lambda arg: colorConverter.to_rgba(arg, alpha=0.6)

xs = np.arange(0, 10, 0.4)
verts = []
zs = np.arange(50)
for z in zs:
    ys = np.ones(len(xs))*z
    ys[0], ys[-1] = 0, 0
    verts.append(list(zip(xs, ys)))

poly = PolyCollection(verts,facecolor='c')
poly.set_alpha(0.7)
ax.add_collection3d(poly, zs=zs, zdir='y')

ax.set_xlabel('X')
ax.set_xlim3d(0, 10)
ax.set_ylabel('Y')
ax.set_ylim3d(-1, len(zs))
ax.set_yticks(np.arange(len(zs)))
labels = {}
for l_c in zs:
    labels[l_c] = 'This Looks Bad'
ax.set_yticklabels(labels,rotation=-15)
ax.set_zlabel('Z')
ax.set_zlim3d(0, ys.max())

plt.show()

在此处输入图片说明

So the question is: how can I get the tick labels to align with the tick positions?所以问题是:如何让刻度标签与刻度位置对齐?

By using these alignments, I get much better placements:通过使用这些对齐方式,我得到了更好的展示位置:

ax.set_yticklabels(labels,rotation=-15,
                   verticalalignment='baseline',
                   horizontalalignment='left')

I've modified the example with less tick markers so you can see the placement:我用较少的刻度标记修改了示例,以便您可以看到位置:

在此处输入图片说明

They do align, but with the horizontal position centered at the tick.它们确实对齐,但水平位置以刻度为中心。 Because of the 3D view this makes them appear a bit below where you would expect them to be.由于 3D 视图,这使得它们看起来比您期望的位置低一点。 The effect is not related to the amount of ticks but to the width.效果与刻度的数量无关,而与宽度有关。

Specifically setting the alignment will help.具体设置对齐方式会有所帮助。 Try adding:尝试添加:

ax.set_yticklabels(labels,rotation=-15, va='center', ha='left')

Play around a bit with the different alignments to see which you prefer, i think you're after ha='left'.尝试使用不同的对齐方式来查看您喜欢哪种对齐方式,我认为您在追求 ha='left'。

Reducing the padding, distance from the tick, might also help.减少填充,与蜱的距离,也可能有所帮助。

在此处输入图片说明

You can also set the pad argument as negative in the tick_params options for each axis.您还可以在每个轴的 tick_params 选项中将 pad 参数设置为负数。 Like this:像这样:

    ax.tick_params(axis='x', which='major', pad=-3)

This might help to adjust the distance between tick labels and axis.这可能有助于调整刻度标签和轴之间的距离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM