简体   繁体   English

Python & Matplotlib:将 x 轴置于中心,但刻度和标签不跟随 x 轴

[英]Python & Matplotlib: put x-axis in centre but tick and label is not followed to the x-axis

In Matplotlib , I want to put x-axis always go through (0, 0).Matplotlib ,我想让 x 轴始终通过 (0, 0)。 Followed is a small code.接下来是一个小代码。 As you can see, x-axis is adjusted to the middle of figure.如您所见,x 轴调整到图形的中间。 What I want is x-axis shown at (0, 1.5) always at (0,0).我想要的是在 (0, 1.5) 处显示的 x 轴总是在 (0,0) 处。

import pandas as pd
import matplotlib.pyplot as plt  

fig = plt.figure()
ax = fig.add_subplot(111)

x=[0,2.7,5.4]
y=[0,3.0,0]
ax.plot(x, y)

ax.spines['bottom'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
#  ax.spines['bottom'].set_color('none')

plt.gca().invert_yaxis()
plt.show()

在此处输入图片说明

From the docs , use 'zero' when setting the position of the spine:文档中,设置脊椎的位置时使用'zero'

ax.spines['bottom'].set_position('zero')

Output:输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM