为了账号安全,请及时绑定邮箱和手机立即绑定

更改小平面网格上的 x 轴或设置fill_between限制

更改小平面网格上的 x 轴或设置fill_between限制

翻阅古今 2022-09-06 21:30:18
我已经复制了RidgePlot示例,并根据需要采用了它。我已经包含了工作脚本和一些示例数据进行测试。图像:我的一些数据的 y 值等于 0.0。我想在我的情节中强调这一点。我不确定绘图的哪个部分,或者只是每个x轴都可以更改,例如删除那些地方的映射颜色。fill_between"aandeel" == 0.0关于如何在这里删除颜色的任何想法?是否可以简单地删除x轴/将宽度设置为0/将颜色更改为不同?示例数据 (csv):https://gist.github.com/willemvanopstal/c2892e68d6eb94194acd371e49d949bdimport pandas as pdfrom matplotlib import pyplot as pltimport seaborn as snsimport numpy as npsns.set(style="whitegrid", rc={"axes.facecolor": (0, 0, 0, 0)})data = pd.read_csv('stats_together_data.csv', delimiter=';')df = data# Initialize the FacetGrid objectpal = sns.color_palette("Set2")g = sns.FacetGrid(df, row="process", hue="process", aspect=10, height=0.6, palette=pal)# Draw the densities in a few stepsg.map(sns.lineplot, "region", "aandeel", clip_on=False, alpha=1, lw=1.5)g.map(plt.fill_between, "region", "aandeel", interpolate=True)g.map(sns.lineplot, "region", "aandeel", clip_on=False, color="w", lw=2)g.map(plt.axhline, y=0, lw=1, clip_on=False)# Define and use a simple function to label the plot in axes coordinatesdef label(x, color, label):    ax = plt.gca()    ax.text(0, .2, label, fontweight="bold", color=color,            ha="left", va="center", transform=ax.transAxes)g.map(label, "region")# Set the subplots to overlapg.fig.subplots_adjust(hspace=-.25)# Remove axes details that don't play well with overlapg.set_titles("")g.set(yticks=[])g.set(xticks=df.region[0::1])g.despine(bottom=True, left=True)plt.show()
查看完整描述

1 回答

?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

首先,我将 0.0 值替换为 NaN。但这还不够,因为问题的很大一部分是添加的水平线,以替换底部书脊,这掩盖了曲线在不同点停止的事实。如果删除这些内容,则只剩下:

//img1.sycdn.imooc.com//63174b990001503f11190334.jpg

就个人而言,我并没有过分发现刻度白线,这使得左右的狭窄区域非常薄。我倾向于删除它们:

//img1.sycdn.imooc.com//63174ba30001bbcf11100334.jpg

完整代码:


import pandas as pd

from matplotlib import pyplot as plt

import seaborn as sns

import numpy as np



sns.set(style="whitegrid", rc={"axes.facecolor": (0, 0, 0, 0)})

df = pd.read_csv('https://gist.githubusercontent.com/willemvanopstal/c2892e68d6eb94194acd371e49d949bd/raw/642c4261198f0cacdf32aad21aebca5953c6cd75/stats_together_data.csv', delimiter=';')

df.loc[df["aandeel"]==0,"aandeel"]=np.nan


# Initialize the FacetGrid object

pal = sns.color_palette("Set2")

g = sns.FacetGrid(df, row="process", hue="process", aspect=10, height=0.6, palette=pal)


# Draw the densities in a few steps

g.map(sns.lineplot, "region", "aandeel", clip_on=False, alpha=1, lw=1.5)

g.map(plt.fill_between, "region", "aandeel", interpolate=True)

#g.map(sns.lineplot, "region", "aandeel", clip_on=False, color="w", lw=2)



# Define and use a simple function to label the plot in axes coordinates

def label(x, color, label):

    ax = plt.gca()

    ax.text(0, .2, label, fontweight="bold", color=color,

            ha="left", va="center", transform=ax.transAxes)



g.map(label, "region")


# Set the subplots to overlap

g.fig.subplots_adjust(hspace=-.25)


# Remove axes details that don't play well with overlap

g.set_titles("")

g.set(yticks=[])

g.set(xticks=df.region[0::1])

g.despine(bottom=True, left=True)


plt.show()



查看完整回答
反对 回复 2022-09-06
  • 1 回答
  • 0 关注
  • 72 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信