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

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

问题描述

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

我尝试了以下代码

我想画的东西如下(

这个问题 描述了一个类似的问题,但它在中间绘制了一条参考线但没有轴.

解决方案

一种方法是使用 spines:

导入数学将 numpy 导入为 np导入 matplotlib.pyplot 作为 plt定义 sigmoid(x):a = []对于 x 中的项目:a.append(1/(1+math.exp(-item)))返回一个x = np.arange(-10., 10., 0.2)sig = sigmoid(x)fig = plt.figure()ax = fig.add_subplot(1, 1, 1)# 将左 y 轴和下 x 轴移动到中心,通过 (0,0)ax.spines['left'].set_position('center')ax.spines['bottom'].set_position('center')# 消除上轴和右轴ax.spines['right'].set_color('none')ax.spines['top'].set_color('none')# 只在左轴和下轴显示刻度ax.xaxis.set_ticks_position('底部')ax.yaxis.set_ticks_position('left')plt.plot(x,sig)plt.show()

显示:

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.

解决方案

One way to do it is using 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()

shows:

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

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