python-2.7 - 如何用seaborn填充一条线的两侧?
问题描述
我需要在图表的两侧填充不同的颜色(可能是绿色和红色)。我正在使用以下代码:
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib
range_x = [-1, 0, 1, 2]
range_y = [-5, -3, -1, 1]
ax = sns.lineplot(x = range_x, y = range_y, markers = True)
sns.lineplot(ax = ax, x = [range_x[0], range_x[-1]], y = [0, 0], color = 'black')
sns.lineplot(ax = ax, x = [0, 0], y = [range_y[0], range_y[-1]], color = 'black')
ax.fill_between(range_x, range_y, facecolor = 'red', alpha = 0.5)
plt.savefig('test_fig', bbox_inches = 'tight')
plt.close()
使用该代码,我得到下图:
但显然这是失败的,因为我想要蓝色线以上的红色。除了我想要我的 x 和 y 轴以一种非凡的方式,我用 x 轴得到它,但我不知道为什么我不能用 y 轴得到它。
非常感谢您提前!
解决方案
像这样的东西?:
ax = sns.lineplot(x = range_x, y = range_y, markers = True)
sns.lineplot(ax = ax, x = [range_x[0], range_x[-1]], y = [0, 0], color = 'black')
sns.lineplot(ax = ax, x = [0, 0], y = [range_y[0], range_y[-1]], color = 'black')
ax.fill_between(range_x, range_y,[ax.get_ylim()[1]]*len(range_x), facecolor = 'red', alpha = 0.5)
ax.fill_between(range_x, range_y,[ax.get_ylim()[0]]*len(range_x), facecolor = 'green', alpha = 0.5)
从以下文档:fill_between
y2 : 数组(长度 N)或标量,可选,默认值:0 定义第二条曲线的节点的 y 坐标。
推荐阅读
- ubuntu - 如何在 Metasploitable 虚拟机中调出 eth0?
- python - 格式化满足条件的行 Pandas Python
- macos - 新的 Mac M1 (ARM) 芯片如何安装 QMK
- php - 仅获取当前月份的两个日期之间的天数
- c - 我无法在 C 中执行代码 | 某种不显示的错误
- javascript - 您可以在 calc 中访问 css 类自己的属性吗?
- x86-64 - 为什么循环从 Uop Cache 提供的 uops 过渡到 LSD 会导致分支未命中率激增?
- android - 科尔多瓦模拟android和Gradle的问题
- json.net - JSON.net 将 json 文本从平面反序列化为嵌套对象
- join - 使用 TMap 从多个表验证 talend 外键是否存在