Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在seaborn的边际单变量图上用黑色虚线和数字进行注释_Python_Matplotlib_Seaborn - Fatal编程技术网

Python 如何在seaborn的边际单变量图上用黑色虚线和数字进行注释

Python 如何在seaborn的边际单变量图上用黑色虚线和数字进行注释,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,这是我的密码: 导入seaborn作为sns 将matplotlib.pyplot作为plt导入 从scipy导入统计信息 tips=sns.load_数据集(“tips”) sns.set(style=“ticks”,color\u code=True) g=sns.JointGrid(x=“总账单”,y=“小费”,数据=小费) g、 plot_joint(sns.regplot,line_kws={“color”:“coral”},scatter_kws={“color”:“gray”})

这是我的密码:

导入seaborn作为sns
将matplotlib.pyplot作为plt导入
从scipy导入统计信息
tips=sns.load_数据集(“tips”)
sns.set(style=“ticks”,color\u code=True)
g=sns.JointGrid(x=“总账单”,y=“小费”,数据=小费)
g、 plot_joint(sns.regplot,line_kws={“color”:“coral”},scatter_kws={“color”:“gray”})
plt.grid()
rsquare=lambda a,b:stats.pearsonr(a,b)[0]**2
g=g.annotate(rsquare,template=“{stat}:{val:.2f}”,stat=“$R^2$”,loc=“左上角”,fontsize=12)
g=g.plot_marginals(sns.kdeplot,color=“steelblue”,shade=True,线宽=3)
marg_x=g.ax_marg_x
marg_y=g.ax_marg_y
marg_x.annotate('15',xy=(15,0),xytext=(15,1),arrowprops=dict(facecolor='k',arrowstyle='-'))
marg_x.annotate('25',xy=(25,0),xytext=(25,1),arrowprops=dict(facecolor='k',arrowstyle='-'))
marg_y.annotate('2',xy=(0,2),xytext=(1,2),arrowprops=dict(facecolor='k',arrowstyle='-'))
marg_y.annotate('4',xy=(0,4),xytext=(1,4),arrowprops=dict(facecolor='k',arrowstyle='-'))
plt.show()
正如我们从代码中看到的,我想用黑线注释marg_x和marg_y,但似乎marg_x和marg_y都用白线注释,数字('15'、'25'、'2'、'4')根本不显示。另外,我更喜欢虚线。我应该如何注释

PS:当我运行代码时,我得到如下警告:

D:\pycharm\pycharm 2018.3\helpers\pycharm\u matplotlib\u backend\backend\u interagg.py:62:UserWarning:未应用紧凑布局。底部和顶部边距不能大到足以容纳所有轴装饰。 self.figure.tight_布局() 这个警告与我的问题有关吗


我发现有一个简单的方法,那就是使用axis.plot。事实上,这是我想到的第一个答案,但当时我设置了错误的参数,所以我切换到Axes.annotate。以下是最终代码

导入seaborn作为sns
将matplotlib.pyplot作为plt导入
从scipy导入统计信息
tips=sns.load_数据集(“tips”)
sns.set(style=“ticks”,color\u code=True)
g=sns.JointGrid(x=“总账单”,y=“小费”,数据=小费)
g、 plot_joint(sns.regplot,line_kws={“color”:“coral”},scatter_kws={“color”:“gray”})
plt.grid()
rsquare=lambda a,b:stats.pearsonr(a,b)[0]**2
g=g.annotate(rsquare,template=“{stat}:{val:.2f}”,stat=“$R^2$”,loc=“左上角”,fontsize=12)
g=g.plot_marginals(sns.kdeplot,color=“steelblue”,shade=True,线宽=3)
marg_x=g.ax_marg_x
marg_y=g.ax_marg_y
x=[2,2]
y=[0,0.05]
marg_x.图(x,y,'k--')
x=[4,4]
y=[0,0.05]
marg_x.图(x,y,'k--')
y=[4,4]
x=[0,0.4]
marg_y.图(x,y,'k--')
y=[7,7]
x=[0,0.4]
marg_y.图(x,y,'k--')
plt.show()

请注意,对于y边距,数据需要在y坐标中(x是水平的,y是垂直轴)。谢谢您的回答!我更改了代码,现在可以同时注释marg_x和marg_y了。你能教我如何让数字显示并用黑色虚线注释吗?