【问题标题】:How to despine a matplotlib and seaborn axes如何定义 matplotlib 和 seaborn 轴
【发布时间】:2014-03-31 16:26:14
【问题描述】:

我很难在绘图中打开和关闭坐标轴,也很难调整坐标轴的大小。我关注了几个线程,我的方法是:

f1=plt.figure(1,(3,3))
ax=Subplot(f1,111)
f1.add_subplot(ax)
ax.scatter(current,backg,label='Background Height')
ax.plot(current,backg)
ax.scatter(current,peak,color = 'red',label='Peak Spot Height')
ax.plot(current,peak,color='red')
ax.plot(current,meanspot,color='green')
ax.scatter(current,meanspot,color = 'green',label='Mean Spot Height')

ax.spines['left'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('center')
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

但我的图形最终仍然在顶部和右侧出现轴,并且由于轴的大小而出现奇怪的间隙。

【问题讨论】:

  • 我不太确定你想要达到什么目标,但是 matplotlib 网站上有 some examples 可以帮助你。

标签: python matplotlib seaborn


【解决方案1】:

我强烈建议您查看seaborn 库以进行此类操作。删除刺就像sns.despine() 一样简单。

例如,要制作一个白色背景的无脊线图表,我可能会写

import pandas as pd
import numpy as np
import seaborn as sns

df2 = pd.Series([np.sqrt(x) for x in range(20)])
sns.set(style="nogrid")
df2.plot()
sns.despine(left=True, bottom=True)

得到

查看链接文档以了解更多详细信息。它确实比手动编写所有代码更容易控制 matplotlib 美学。

【讨论】:

    【解决方案2】:

    您能更具体地说明需要什么吗?

    您可以使用以下方法移除脊椎:

    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    

    但听起来你可能指的是脊椎上的蜱虫......

    【讨论】:

      猜你喜欢
      • 2019-02-03
      • 2016-08-25
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 2016-05-21
      相关资源
      最近更新 更多