python sns 画图 双Y轴

您好,请教一下,如何把下图的下方的折线合并到上方的图上。
对第一条线,射中坐标轴范围未 0-1, 放在左边的y轴
对第二条线,射中坐标轴范围未 0-500, 放在右边的y轴

plt.figure(figsize=(19,8))
ax1 = plt.subplot(2, 1, 1)
line1 = sns.lineplot(x="Pinfit_date2",y="ratio", data=df_ct3)
ax1.set_ylabel("ratio")
ax1.set_xlabel("Pinfit_date")
plt.xticks(rotation=-45)
ax2 = ax1.twinx()
ax2 = plt.subplot(2, 1, 2)
#plt.figure(figsize=(19,8))
ax2 = sns.lineplot(x="Pinfit_date2",y="Good", data=df_ct3)
ax2.set_ylabel("Good_QTY")
plt.xticks(rotation=-45)


plt.show()

img

因为不知道你的数据集,我用的随机数据集

上图

img

代码

import matplotlib.pyplot as plt
import seaborn as sns

import numpy as np
import pandas as pd

# 因为不知道你的数据集,我用的随机数据集   2023年7月3日17:53:11
df_ct3 = pd.DataFrame(np.random.rand(100, 2), columns=["Pinfit_date2", "ratio"])
df_ct3["Good"] = np.random.randint(0, 500, 100)
plt.figure(figsize=(19,8))
ax1 = plt.subplot(1, 1, 1) # 创建一个子图
line1 = sns.lineplot(x="Pinfit_date2",y="ratio", data=df_ct3, ax=ax1) # 在子图上绘制第一条折线
ax1.set_ylabel("ratio") # 设置左边的y轴标签
ax1.set_xlabel("Pinfit_date") # 设置x轴标签
plt.xticks(rotation=-45) # 旋转x轴刻度标签

ax2 = ax1.twinx() # 创建一个共享x轴但有不同y轴的子图
line2 = sns.lineplot(x="Pinfit_date2",y="Good", data=df_ct3, ax=ax2, color="orange") # 在子图上绘制第二条折线
ax2.set_ylabel("Good_QTY") # 设置右边的y轴标签

plt.legend([line1, line2], ["ratio", "Good_QTY"], loc="upper left") # 添加图例

plt.show()


基于new bing部分指引作答:
要将下方的折线合并到上方的图上,并使用双Y轴,您可以按照以下步骤进行修改:

import matplotlib.pyplot as plt
import seaborn as sns

plt.figure(figsize=(19, 8))

# 创建第一个子图,上方的图
ax1 = plt.subplot(2, 1, 1)
line1 = sns.lineplot(x="Pinfit_date2", y="ratio", data=df_ct3, ax=ax1)
ax1.set_ylabel("ratio")
ax1.set_xlabel("Pinfit_date")
plt.xticks(rotation=-45)

# 创建第二个子图,下方的图
ax2 = ax1.twinx()
line2 = sns.lineplot(x="Pinfit_date2", y="Good", data=df_ct3, ax=ax2)
ax2.set_ylabel("Good_QTY")

# 调整第二个子图的纵坐标轴范围为0-500
ax2.set_ylim(0, 500)

# 隐藏第二个子图的横坐标轴刻度和标签
ax2.set_xticks([])
ax2.set_xticklabels([])

plt.show()

这段代码将两个子图合并到同一个图中,并使用双Y轴。第一个子图(上方)绘制了"ratio"数据,纵坐标轴范围为0-1,放在左边的Y轴。第二个子图(下方)绘制了"Good"数据,纵坐标轴范围为0-500,放在右边的Y轴。请注意,为了使两个子图的纵坐标轴范围一致,我们使用了ax2.set_ylim(0, 500)设置第二个子图的范围。

希望这可以帮助到您!

  • 以下回答由chatgpt基于相关博客总结生成:

    我可以通过使用matplotlib库和seaborn库来解决这个问题。下面是具体的解决方案:

    1. 导入必要的库
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    1. 创建一个包含两个子图的画布
    fig, ax1 = plt.subplots()
    ax2 = ax1.twinx()
    
    1. 绘制第一条线并设置左边的Y轴范围
    data1 = [0, 0.2, 0.4, 0.6, 0.8, 1]
    ax1.plot(data1, color='r')
    ax1.set_ylim(0, 1)
    
    1. 绘制第二条线并设置右边的Y轴范围
    data2 = [0, 100, 200, 300, 400, 500]
    ax2.plot(data2, color='b')
    ax2.set_ylim(0, 500)
    
    1. 设定横轴标签和标题
    ax1.set_xlabel('X-axis')
    ax1.set_ylabel('Y1-axis')
    ax2.set_ylabel('Y2-axis')
    plt.title('Double Y-axis Plot')
    
    1. 显示图表
    plt.show()
    

    完整代码如下所示:

    import matplotlib.pyplot as plt
    import seaborn as sns
    
    fig, ax1 = plt.subplots()
    ax2 = ax1.twinx()
    
    data1 = [0, 0.2, 0.4, 0.6, 0.8, 1]
    ax1.plot(data1, color='r')
    ax1.set_ylim(0, 1)
    
    data2 = [0, 100, 200, 300, 400, 500]
    ax2.plot(data2, color='b')
    ax2.set_ylim(0, 500)
    
    ax1.set_xlabel('X-axis')
    ax1.set_ylabel('Y1-axis')
    ax2.set_ylabel('Y2-axis')
    plt.title('Double Y-axis Plot')
    
    plt.show()
    

    这个代码将会创建一个包含两个Y轴的图表,并在左边的Y轴上绘制第一条线,在右边的Y轴上绘制第二条线。

要将第二条线合并到第一条线的上方,可以使用ax2.fill_between()函数来填充两条线之间的区域。以下是修改后的代码示例:

import matplotlib.pyplot as plt
import seaborn as sns

plt.figure(figsize=(19, 8))
ax1 = plt.subplot(2, 1, 1)
line1 = sns.lineplot(x="Pinfit_date2", y="ratio", data=df_ct3)
ax1.set_ylabel("ratio")
ax1.set_xlabel("Pinfit_date")
plt.xticks(rotation=-45)
ax2 = ax1.twinx()

ax2.fill_between(df_ct3["Pinfit_date2"], df_ct3["ratio"], 1, color="gray", alpha=0.3)  # 填充两条线之间的区域

ax2.set_ylabel("Good_QTY")
ax2.set_ylim(0, 500)  # 设置右边y轴的范围
plt.xticks(rotation=-45)

plt.show()


在上述代码中,我们添加了ax2.fill_between()函数来填充第一条线和1之间的区域。通过设置合适的color和alpha参数,可以调整填充区域的颜色和透明度。另外,使用ax2.set_ylim()函数设置右边y轴的范围,确保第二条线在正确的位置显示。