分享

python,设置gird刻度

 imelee 2017-05-14

You can use ticker to set the tick locations for 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:

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 draw like this.

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

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

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多