Gab*_*iel 5 python plot matplotlib
我有一个很大的代码,可以输出多个图像。有时,情节看起来像这样(请参见下面的MWE):

请注意,x轴上的刻度线几乎彼此重叠,这看起来很糟糕。
我的问题是:为什么matplotlib无法实现这一点,而只是绘制较少的滴答声(通常不会出现问题)?我该如何强制matplotlib以一般方式绘制较少的滴答声(即:我不是只寻求适用于该特定绘图的解决方案,这只是一个示例)?
MWE(如果代码看起来不必要地复杂,那是因为它只是较大代码中的一小部分)
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import random
def scatter_plot(x, y):
ax = plt.subplot(gs[0:2, 0:2])
plt.xlim(min(x), 0.00158)
plt.xlabel('$x$', fontsize=16)
plt.ylabel('$y$', fontsize=16)
ax.minorticks_on()
plt.scatter(x, y)
# Generate random data.
x = [random.random() / 100. for i in xrange(10000)]
y = [random.random() for i in xrange(10000)]
# Define size of output figure.
fig = plt.figure(figsize=(30, 25)) # create the top-level container
gs = gridspec.GridSpec(10, 12) # create a GridSpec object
# Create plot.
scatter_plot(x, y)
# Save plot to file.
fig.tight_layout()
plt.savefig('out.png', dpi=150)
Run Code Online (Sandbox Code Playgroud)
首先import numpy as np调用ax.xaxis.set_ticks(np.arange(xmin, xmax, stepsize)),其中xmin是最小刻度值,xmax是最大刻度值,stepsize是连续刻度之间的差值。使用 可能是有意义的stepsize = (xmax - xmin) / tick_count,其中tick_count是您想要的刻度数。
| 归档时间: |
|
| 查看次数: |
3317 次 |
| 最近记录: |