在matplotlib图中显示原点轴(x,y)

马丁·维格特(Martin Vegter):

我有一个简单的图,我想显示原点轴(x,y)。我已经有网格,但是我需要强调x,y轴。

在此处输入图片说明

这是我的代码:

x = linspace(0.2,10,100)
plot(x, 1/x)
plot(x, log(x))
axis('equal')
grid()

我已经看到了这个问题。可接受的答案建议使用“轴脊柱”,并仅链接到一些示例。但是,该示例使用子图过于复杂。我无法在我的简单示例中弄清楚如何使用“轴脊柱”。

保罗·H:

使用subplots不是太复杂,可能是刺。

笨,简单的方法:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0.2,10,100)
fig, ax = plt.subplots()
ax.plot(x, 1/x)
ax.plot(x, np.log(x))
ax.set_aspect('equal')
ax.grid(True, which='both')

ax.axhline(y=0, color='k')
ax.axvline(x=0, color='k')

我得到:

用斧头

(由于x的下限为零,因此看不到垂直轴。)

选择使用简单的刺

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0.2,10,100)
fig, ax = plt.subplots()
ax.plot(x, 1/x)
ax.plot(x, np.log(x))
ax.set_aspect('equal')
ax.grid(True, which='both')

# set the x-spine (see below for more info on `set_position`)
ax.spines['left'].set_position('zero')

# turn off the right spine/ticks
ax.spines['right'].set_color('none')
ax.yaxis.tick_left()

# set the y-spine
ax.spines['bottom'].set_position('zero')

# turn off the top spine/ticks
ax.spines['top'].set_color('none')
ax.xaxis.tick_bottom()

with_spines

Alternative using seaborn (my favorite)

import numpy as np
import matplotlib.pyplot as plt
import seaborn
seaborn.set(style='ticks')

x = np.linspace(0.2,10,100)
fig, ax = plt.subplots()
ax.plot(x, 1/x)
ax.plot(x, np.log(x))
ax.set_aspect('equal')
ax.grid(True, which='both')
seaborn.despine(ax=ax, offset=0) # the important part here

with_seaborn

Using the set_position method of a spine

Here are the docs for a the set_position method of spines:

Spine position is specified by a 2 tuple of (position type, amount). The position types are:

  • 'outward' : place the spine out from the data area by the specified number of points. (Negative values specify placing the
    spine inward.)

  • 'axes' : place the spine at the specified Axes coordinate (from 0.0-1.0).

  • 'data' : place the spine at the specified data coordinate.

Additionally, shorthand notations define a special positions:

  • 'center' -> ('axes',0.5)
  • 'zero' -> ('data', 0.0)

So you can place, say the left spine anywhere with:

ax.spines['left'].set_position((system, poisition))

其中system是“向外”,“轴”或“数据”和position在该坐标系中的位置。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在高图中以X轴显示日期,以y轴显示其他列

通过原点绘制x轴和y轴(MATLAB)

缩放/显示Y轴Matplotlib

如何缩小Seaborn的x轴原点和y轴原点之间的空间

Matplotlib 图中未显示轴尖峰

在 matplotlib 子图中共享 Y 轴

如何在chart.js3折线图中沿y轴方向移动原点,使x轴通过(0,0)以外的不同点?

旋转Matplotlib寄生图中的x轴标签

Matplotlib在使用辅助y轴时未在前两个子图中显示xlabel

如何使用辅助y轴显示每个matplotlib子图的x轴值

无法在高图中以x轴显示日期

检查点是否位于 x 轴、y 轴或原点的程序

Matplotlib:自动将时间列显示为散点图中y轴上的2小时刻度

如何使用Matplotlib突出显示多轴图中的选定线

用双y轴在Matplotlib图中的条形前面排列线

matplotlib:在堆积的散点图中对齐y轴标签

在图形的原点开始 Y 刻度,并在轴在 matplotlib pyplot 中结束的位置结束刻度

Matplotlib:将轴设置为仅紧靠x或y轴

使用matplotlib删除x轴和y轴黑色线条

使用 matplotlib 绘制不同的 x 轴和 y 轴

在matplotlib python中从x轴查找与y轴对应的值

如何调整 x 轴的大小并使其与 y 轴不同 Matplotlib

如何从seaborn / matplotlib图中删除或隐藏x轴标签?

更改Pandas / Matplotlib直方图中标签的x轴顺序?

Matplotlib在所有子图中显示x-ticks和唯一的y标签

如何在matplotlib的3D表面图中将颜色显示为距原点距离的函数?

显示X和Y轴标签?

如何在X的顶部显示Y轴?

显示x轴中断的y值