2

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

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')

但是我的图形最终仍然在顶部和右侧带有轴,并且由于轴的大小而出现了奇怪的间隙。在此处输入图像描述

4

2 回答 2

12

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

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

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

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

于 2018-01-08T20:59:53.037 回答
2

我强烈建议您查看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 美学。

于 2014-02-28T09:54:01.670 回答