【问题标题】:Editing axis lines in matplotlib在 matplotlib 中编辑轴线
【发布时间】:2019-09-20 17:35:06
【问题描述】:

我想删除我的 matplotlib 图的 y 轴(可能通过 pandas DataFrame.plot() 方法),使其不可见并将 x 轴更改为虚线。我见过的最接近这一点的是 pyplot.box(False) 语句,但是它不允许我只选择 y 轴,而且我仍然无法按照描述如何编辑 x 轴。我该怎么做?

【问题讨论】:

  • this 有什么帮助吗?

标签: python python-2.7 matplotlib format axes


【解决方案1】:

这是一种方法。我正在选择一个示例 DataFrame 来绘制。诀窍是使用here@ImportanceOfBeingEarnest建议的方法将左、右和上轴的脊椎隐藏起来,并将下x轴变成虚线

import pandas as pd
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

df = pd.DataFrame({'pig': [20, 18, 489, 675, 1776],
                   'horse': [4, 25, 281, 600, 1900]
                  }, index=[1990, 1997, 2003, 2009, 2014])
ax_ = df.plot.line(ax=ax)

for spine in ['right', 'top', 'left']:
    ax_.spines[spine].set_visible(False)

ax_.spines['bottom'].set_linestyle((0,(8,5)))
plt.yticks([])
plt.show()

【讨论】:

  • 是的,这完全解决了我的问题,刺确实非常有用!
猜你喜欢
  • 2012-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-02
  • 1970-01-01
  • 2016-06-22
  • 1970-01-01
相关资源
最近更新 更多