简体   繁体   English

为什么 matplotlib 中的两个子图都没有显示刻度线?

[英]Why aren't both subplots showing tick marks in matplotlib?

So I'm trying to plot some data via a scatter graph, and I would expect matplotlib to automatically add tick marks, which it does do on the first subplot.所以我试图通过散点图 plot 一些数据,我希望 matplotlib 自动添加刻度线,它确实在第一个子图上做。 However, the second subplot does not have ticks with labels added automatically.但是,第二个子图没有自动添加标签的刻度。 I have tried setting the ticks myself explicitly which didn't work (using xticks and xticklabels).我尝试过自己明确设置刻度,但不起作用(使用 xticks 和 xticklabels)。

Below is my code:下面是我的代码:

axs[0].scatter(dates, yvar, color="black", label="Data")
axs[0].plot(dates, predictions, color="blue", label="Predicted Value")
axs[1].scatter(predictions, yvar, color="red", label="Prediction vs Data")
plt.xticks(())
plt.yticks(())
fig.legend()
plt.show()

This code, trying to replicate your problem, behaves as expected.此代码试图复制您的问题,其行为符合预期。 It produces the matplotlib.pyplot.xticks :它产生matplotlib.pyplot.xticks

Try to call your legend with each ax individually试着用每把ax分别称呼你的传奇

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 360, 1)
y = np.sin(2 * x * np.pi / 180)
z = np.cos(2 * x * np.pi / 180)
fig, ax = plt.subplots(2)
ax[0].scatter(x, y, label='label1')
ax[0].plot(x, z, label='label2')
ax[1].scatter(y, z, label='label3')
ax[0].legend()
ax[1].legend()
plt.tight_layout()
plt.show()

例子

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

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