简体   繁体   English

使用网格 Matplotlib 确定绘图大小

[英]Determine plot size with grid Matplotlib

I'm plotting some data using plt.scatter() , and I want to change the size of the plot which is it on, however the only results which come up when you search 'change plot size' are things to do with changing the figure's size, which I am not looking to do.我正在使用plt.scatter()绘制一些数据,并且我想更改它所在的绘图的大小,但是当您搜索“更改绘图大小”时出现的唯一结果与更改图的大小,我不想这样做。

To visualise my issue, I have a reproducible example where I'm trying to plot 4 points on a 10x10 grid, however the size of the scatter plot is determined by the data not the grid为了可视化我的问题,我有一个可重复的示例,我试图在 10x10 网格上绘制 4 个点,但是散点图的大小由数据而不是网格决定

在此处输入图片说明 在此处输入图片说明

The two graphs above demonstrate my problem, I am trying to plot the four points on the left graph on the 10x10 grid seen on the right graph.上面的两个图展示了我的问题,我试图在右侧图上看到的 10x10 网格上绘制左侧图上的四个点。 I have added in a datapoint at (10, 10) to show this.我在 (10, 10) 添加了一个数据点来显示这一点。

My code is currently:我的代码目前是:

x = [1, 2, 3, 4]
y = [1, 2, 3, 4]
fig = plt.figure()
ax = fig.gca()
ax.set_xticks(np.arange(0, 11, 1))
ax.set_yticks(np.arange(0, 11, 1))
plt.grid()
plt.scatter(x, y)

Which produces the left graph.产生左图。

IIUC: IUC:

x = [1, 2, 3, 4]
y = [1, 2, 3, 4]
fig = plt.figure()
plt.xlim(0, 10)
plt.ylim(0, 10)
plt.grid()
plt.scatter(x, y)

Output:输出:

在此处输入图片说明

Just change the limits of x and y axes:只需更改 x 和 y 轴的限制:

plt.xlim(0,11)
plt.ylim(0,11)

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

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