蜱和蜱标签的独立间距?

时间:2017-06-17 22:08:01

标签: python-3.x matplotlib

我希望有一个带刻度的散点图作为边距:

x = [ 0,  1, 1.2, 1.3, 4, 5, 6, 7, 8.2, 9, 10]
y = [.2, .4, 2, 3, 4, 5, 5.1, 5.2, 4, 3, 8]

fig, ax1 = plt.subplots()
for spine in ax1.spines.values():
    spine.set_visible(False)

ax1.scatter(x, y)

ax1.set_xticks(x)
ax1.set_xticklabels([])

ax1.set_yticks(y)
ax1.set_yticklabels([])

ticks as marginals

最重要的是,我希望在其他位置使用ticklabels,而不是由刻度线确定:

xticklabels = [0, 5, 10]
yticklabels = xticklabels

我怎么可能实现这个目标?

1 个答案:

答案 0 :(得分:1)

Matplotlib轴有主要和次要刻度。您可以使用次要刻度来显示点的边缘位置。您可以关闭主要刻度,但显示它们的刻度标签。

要在某些位置设置滴答,您可以使用FixedLocator。要更改刻度线的外观或将其关闭,轴将采用tick_params方法。

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

x = [ 0,  1, 1.2, 1.3, 4, 5, 6, 7, 8.2, 9, 10]
y = [.2, .4, 2, 3, 4, 5, 5.1, 5.2, 4, 3, 8]

xticklabels = [0, 5, 10]
yticklabels = xticklabels

fig, ax = plt.subplots()
for spine in ax.spines.values():
    spine.set_visible(False)

ax.scatter(x, y)

ax.xaxis.set_major_locator(ticker.FixedLocator(xticklabels))
ax.yaxis.set_major_locator(ticker.FixedLocator(yticklabels))

ax.xaxis.set_minor_locator(ticker.FixedLocator(x))
ax.yaxis.set_minor_locator(ticker.FixedLocator(y))

ax.tick_params(axis="both", which="major", bottom="off", left="off")
ax.tick_params(axis="both", which="minor", length=4)

plt.show()

enter image description here

请注意,我个人觉得这个情节很难掌握,如果可以的话,我会提出更像这样的事情:

enter image description here

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

x = [ 0,  1, 1.2, 1.3, 4, 5, 6, 7, 8.2, 9, 10]
y = [.2, .4, 2, 3, 4, 5, 5.1, 5.2, 4, 3, 8]

xticklabels = [0, 5, 10]
yticklabels = xticklabels

fig, ax = plt.subplots()

ax.scatter(x, y)

ax.xaxis.set_minor_locator(ticker.FixedLocator(x))
ax.yaxis.set_minor_locator(ticker.FixedLocator(y))

c = "#aaaaaa"
ax.tick_params(axis="both", which="major", direction="out", color=c)
ax.tick_params(axis="both", which="minor", length=6, direction="in",
           color="C0", width=1.5)
plt.setp(ax.spines.values(), color=c)
plt.setp(ax.get_xticklabels(), color=c)
plt.setp(ax.get_yticklabels(), color=c)

plt.show()