问题描述
我有一个简单的图,我想显示原点轴(x,y).我已经有网格了,但是我需要强调x,y轴.
I have following simple plot, and I would like to display the origin axis (x, y). I already have grid, but I need the x, y axis to be emphasized.
这是我的代码:
x = linspace(0.2,10,100)
plot(x, 1/x)
plot(x, log(x))
axis('equal')
grid()
我已经看到了这个问题.可接受的答案建议使用轴脊柱",并仅链接到一些示例.但是,该示例使用子图过于复杂.我无法在我的简单示例中弄清楚如何使用轴脊柱".
I have seen this question. The accepted answer suggests to use "Axis spine" and just links to some example. The example is however too complicated, using subplots. I am unable to figure out, how to use "Axis spine" in my simple example.
推荐答案
使用subplots
不太复杂,可能是棘刺.
Using subplots
is not too complicated, the spines might be.
%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的下限为零,因此您看不到垂直轴.)
(you can't see the vertical axis since the lower x-limit is zero.)
%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()
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
以下是 set_position
方法的文档的文档:
Here are the docs for a the set_position
method of spines:
-
'outward':将书脊从数据区域中移出指定的点数. (负值指定放置
脊椎向内.)
'outward' : place the spine out from the data area by the specified number of points. (Negative values specify placing the
spine inward.)
'axes':将书脊放置在指定的Axes坐标处(从 0.0-1.0).
'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)
- '零'->('数据',0.0)
因此,您可以使用以下任何一种方法放置左脊椎:
So you can place, say the left spine anywhere with:
ax.spines['left'].set_position((system, poisition))
其中system
是向外",轴"或数据",而position
在该坐标系中的位置.
where system
is 'outward', 'axes', or 'data' and position
in the place in that coordinate system.
这篇关于在matplotlib图中显示原点轴(x,y)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!