Python PyPlot - 设置绘图的网格线间距

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30482727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:29:41  来源:igfitidea点击:

PyPlot - Setting grid line spacing for plot

pythonmatplotlibnetworkx

提问by HFulcher

I have an undirected graph created by Networkx that I am displaying using pyplot and I want to allow the user to specify the spacing between grid lines. I don't want to manually enter the ticks as this requires knowing the final size of the plot (if there's a way to do this I would like to know) which could vary based on the graph being displayed.

我有一个由 Networkx 创建的无向图,我正在使用 pyplot 显示它,我希望允许用户指定网格线之间的间距。我不想手动输入刻度,因为这需要知道绘图的最终大小(如果有办法做到这一点,我想知道),这可能会根据显示的图形而有所不同。

Is there any method that allows you to set the spacing amount? I've looked for a while and can't find anything, thanks.

有没有什么方法可以让你设置间距量?找了好久都没找到,谢谢。

The code below relates to the creating of the plot not the graph.

下面的代码与绘图而不是图形的创建有关。

#Spacing between each line
intervals = float(sys.argv[1])

nx.draw(displayGraph, pos, node_size = 10)
plt.axis('on')
plt.grid('on')
plt.savefig("test1.png")

I need to find a way to get the grid to have spacing intervals that are defined by the user. I've found ways to do it but it relies on also saying how many grid lines you want and that causes the lines to not be evenly spaced over the plot

我需要找到一种方法让网格具有用户定义的间距。我已经找到了方法来做到这一点,但它也依赖于说明你想要多少条网格线,这会导致线条在图上不均匀分布

采纳答案by tmdavison

Not sure if this contravenes your desire not to manually play with ticks, but you can use matplotlib.tickerto set the ticks to your given interval:

不确定这是否违反了您不想手动使用刻度的愿望,但您可以使用matplotlib.ticker将刻度设置为给定的间隔:

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

fig,ax=plt.subplots()

#Spacing between each line
intervals = float(sys.argv[1])

loc = plticker.MultipleLocator(base=intervals)
ax.xaxis.set_major_locator(loc)
ax.yaxis.set_major_locator(loc)

# Add the grid
ax.grid(which='major', axis='both', linestyle='-')

回答by otterb

You can for example use xlimto get the range of x-axis and with the user specified interval, you should be able to draw the grid yourself using ax.axvlineas in this example https://stackoverflow.com/a/9128244/566035

例如,您可以使用xlim获取 x 轴的范围和用户指定的间隔,您应该能够自己绘制网格,ax.axvline如本例所示https://stackoverflow.com/a/9128244/566035

hope this help.

希望这有帮助。

(edit) here's a sample.

(编辑)这是一个示例。

from pylab import *

fig = figure()
ax = fig.add_subplot(111)

ax.plot([1,2,3, 15],[2,3,4, 25],'ro')
xmin,xmax = xlim()
user_interval = 1.5
for _x in np.arange(xmin, xmax, user_interval):
    ax.axvline(x=_x, ls='-')
draw()

回答by Molly

You can use tickerto set the tick locationsfor the grid. The user can specify the input to MultipleLocator which will "Set a tick on every integer that is multiple of base in the view interval." Here's an example:

您可以使用ticker来设置网格的刻度位置。用户可以指定 MultipleLocator 的输入,它将“在视图间隔中为基数的倍数的每个整数设置一个刻度”。下面是一个例子:

from matplotlib import pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np

# Two example plots
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)

spacing = 0.5 # This can be your user specified spacing. 
minorLocator = MultipleLocator(spacing)
ax1.plot(9 * np.random.rand(10))
# Set minor tick locations.
ax1.yaxis.set_minor_locator(minorLocator)
ax1.xaxis.set_minor_locator(minorLocator)
# Set grid to use minor tick locations. 
ax1.grid(which = 'minor')

spacing = 1
minorLocator = MultipleLocator(spacing)
ax2.plot(9 * np.random.rand(10))
# Set minor tick locations.
ax2.yaxis.set_minor_locator(minorLocator)
ax2.xaxis.set_minor_locator(minorLocator)
# Set grid to use minor tick locations. 
ax2.grid(which = 'minor')

plt.show()

Two subplots with different grids.

具有不同网格的两个子图。

Edit

编辑

To use this along with Networkx you can either create the axes using subplot (or some other function) as above and pass that axes to drawlike this.

要将它与 Networkx 一起使用,您可以使用上面的子图(或其他一些函数)创建轴,并传递该轴以这样绘制

nx.draw(displayGraph, pos, ax=ax1, node_size = 10)

Or you can call nx.draw as you do in your question and use gcato get the current axis afterwards:

或者您可以像在问题中一样调用 nx.draw ,然后使用gca获取当前轴:

nx.draw(displayGraph, pos, node_size = 10)
ax1 = plt.gca()