如何使用 matplotlib 和 seaborn 轴

KRS*_*fun 2 python matplotlib seaborn

我很难在图中打开和关闭轴,以及适当调整轴的大小。我遵循了几个线程,我的方法是:

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')
Run Code Online (Sandbox Code Playgroud)

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

小智 9

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

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

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
Run Code Online (Sandbox Code Playgroud)

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


jmz*_*jmz 6

我强烈建议您查看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)
Run Code Online (Sandbox Code Playgroud)

要得到

在此输入图像描述

请查看链接文档以了解更多详细信息。与手动编写所有代码相比,它确实使控制 matplotlib 的美观变得更加容易。