惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
J
Java Code Geeks
Jina AI
Jina AI
罗磊的独立博客
宝玉的分享
宝玉的分享
S
SegmentFault 最新的问题
D
DataBreaches.Net
博客园 - 叶小钗
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
B
Blog
V
Visual Studio Blog
雷峰网
雷峰网
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
How to Change Intersection Point of X and Y Axis in Matpl...
2018-05-21 · via jdhao's digital space

In this post, I will share how to position the intersection of x and y axis at a specific point using Matplotlib.

The spines#

In order to re-position x and y axis, we need to understand an important concept in Matplotlib —spines. According to Matplotlib documentation:

Spines are the lines connecting the axis tick marks and noting the boundaries of the data area. They can be placed at arbitrary positions. See function:set_position for more information.

In normal plot, we can clearly see four spines around the plot. Like the following plot:

Two of the 4 spines, i.e., bottom and left spines are used as x and y axis. Another two spines are used to indicate data boundary.

Move the spines#

In order to move a specific spine, we use the set_position() method of spines. This method accepts a 2-tuple as its parameter. The first element of the tuple is used to indicate the desired coordinate system1, i.e., axes or data. If you use axes, the value for the second element is in the range $[0.0, 1.0]$. If you choose data, the value for the second element is the range of actual data range.

We can use ax.spines to get the four spines. The returned object will be a dict, whose keys are bottom, left, top, right. Then we use one of these keys to access a specific spine and customize it.

Position spines in data coordinate#

Let us take a concrete example. Suppose we want to put the intersection of x and y axis at data coordinate $(0, 0)$, we can use the following snippet:

ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))

The produced image will be:

The image looks a bit ugly since top and right spines now seem redundant. How do we remove them? It is easy:

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

Now the image will be prettier:

You can move the spines to whatever position you want, see more example below:

Position spines in axes coordinate#

We can also position spines using the axes coordinate system. For example, if you want to place the x and y axis in the top right of the axes, i.e., in the axes coordinate $(1, 1)$, use the following script:

ax.spines['left'].set_position(('axes', 1))
ax.spines['bottom'].set_position(('axes', 1))

The produced plot is

References#