【发布时间】:2021-09-21 04:23:43
【问题描述】:
我正在 matplotlib 中创建子图,但并未显示所有 xticks 和 yticks。我已经尝试了一切,从设置 xlim 和 ylim,链接图形大小等。问题是这是对 hackerrnak 的动手操作,他们正在根据他们的预期输出评估我的输出。 xaxis 中的 0.0 和 yaxis 中的 1.0 根本不匹配。我在这里做错了什么。
这是代码,
import matplotlib.pyplot as plt
import numpy as np
def test_generate_figure2():
np.random.seed(1000)
x = np.random.rand(10)
y = np.random.rand(10)
z = np.sqrt(x**2 + y**2)
fig = plt.figure(figsize=(8,6))
axes1 = plt.subplot(2, 2, 1, title="Scatter plot with Upper Triangle Markers")
axes1.set_xticks([0.0, 0.4, 0.8, 1.2])
axes1.set_yticks([-0.2, 0.2, 0.6, 1.0])
axes1.set_ylim(-0.2,1.0) #Doing this still doesnot get the expected output
axes1.set_xlim(0.0,1.2)
print(axes1.get_yticks())
axes1.scatter(x, y, marker="^", s=80, c=z)
axes2 = plt.subplot(2, 2, 2, title="Scatter plot with Plus Markers")
axes2.set_xticks([0.0, 0.4, 0.8, 1.2])
axes2.set_yticks([-0.2, 0.2, 0.6, 1.0])
axes2.scatter(x, y, marker="+", s=80, c=z)
axes3 = plt.subplot(2, 2, 3, title="Scatter plot with Circle Markers")
axes3.set_xticks([0.0, 0.4, 0.8, 1.2])
axes3.set_yticks([-0.2, 0.2, 0.6, 1.0])
axes3.scatter(x, y, marker="o", s=80, c=z)
axes4 = plt.subplot(2, 2, 4, title="Scatter plot with Diamond Markers")
axes4.set_xticks([0.0, 0.4, 0.8, 1.2])
axes4.set_yticks([-0.2, 0.2, 0.6, 1.0])
axes4.scatter(x, y, marker="d", s=80,c=z)
plt.tight_layout()
plt.show()
test_generate_figure2()
【问题讨论】:
-
你在分散调用之前设置你的刻度;设置它们之后。您可能还需要考虑使用带有 sharex=True 和 shared=True 选项的子图。
-
@JodyKlymak 感谢您的解决方案有效。我在没有绘制任何东西的情况下设置刻度。
-
我投票决定关闭此问题,因为这是由于拼写错误/不可重现,因为作者表示@JodyKlymak 的评论解决了这个问题。对此answer 发表了评论。
标签: python-3.x matplotlib