如何在图的中间绘制轴? [英] How to draw axis in the middle of the figure?

查看:85
本文介绍了如何在图的中间绘制轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在matplotib中绘制一个图形,其中坐标轴显示在图形本身而不是侧面

I want to draw a figure in matplotib where the axis are displayed within the plot itself not on the side

我从这里尝试了以下代码 :

I have tried the following code from here:

import math
import numpy as np
import matplotlib.pyplot as plt

def sigmoid(x):
    a = []
    for item in x:
        a.append(1/(1+math.exp(-item)))
    return a

x = np.arange(-10., 10., 0.2)
sig = sigmoid(x)

plt.plot(x,sig)
plt.show()

上面的代码显示如下图:

The above code displays the figure like this:

我想绘制的内容如下(图像来自维基百科)

What I would like to draw is something as follows (image from Wikipedia)

问题描述了类似的问题,但是它在中间但没有轴的位置绘制了参考线.

This question describes a similar problem, but it draws a reference line in the middle but no axis.

推荐答案

一种方法是使用 spines :

import math
import numpy as np
import matplotlib.pyplot as plt

def sigmoid(x):
    a = []
    for item in x:
        a.append(1/(1+math.exp(-item)))
    return a


x = np.arange(-10., 10., 0.2)
sig = sigmoid(x)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

# Move left y-axis and bottim x-axis to centre, passing through (0,0)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')

# Eliminate upper and right axes
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

# Show ticks in the left and lower axes only
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.plot(x,sig)
plt.show()

显示:

这篇关于如何在图的中间绘制轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆