简体   繁体   English

如何创建具有 3 个不同 y 轴的 Seaborn 线图?

[英]How can I create a Seaborn line plot with 3 different y-axis?

I am trying to plot 3 different scales on the y-axis with 3 different sets of data.我试图用 3 组不同的数据在 y 轴上绘制 3 个不同的比例。 I am able to plot the 3rd line but the y2 and y3 axis are together.我能够绘制第三条线,但 y2 和 y3 轴在一起。

I need to separate these two axes so that they are readable.我需要将这两个轴分开,以便它们可读。

在此处输入图片说明

Can this be done with Seaborn Library?这可以通过 Seaborn 库完成吗?

This is the code:这是代码:

 import datetime import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt # Ingest the data url = 'https://covid.ourworldindata.org/data/owid-covid-data.csv' covid_data = pd.read_csv(url).set_index("location") # Clean the data df = covid_data.copy() df.date = pd.to_datetime(df.date) df = df.loc[df['date'] > (datetime.datetime(2021, 4, 30)), :] df = df[df.index.isin(['United States'])] # Select the features of interest new_cases = 'new_cases_smoothed_per_million' patients = 'hosp_patients_per_million' vaccinated = 'people_fully_vaccinated_per_hundred' bedsT = 'hospital_beds_per_thousand' bedsM = 'hospital_beds_per_million' beds_used = 'hospital_beds_used' df = df.loc[:, ['date', new_cases, patients, vaccinated, bedsT]] df[bedsM] = df[bedsT] * 1000 df[beds_used]=df.apply(lambda x: x[patients] / x[bedsM], axis = 1) # Visualise the data y1_color = "red" y2_color = "green" y3_color = "blue" x1_axis = "date" y1_axis = new_cases y2_axis = vaccinated y3_axis = beds_used x1 = df[x1_axis] y1 = df[y1_axis] y2 = df[y2_axis] y3 = df[y3_axis] y2_limit = df[y2_axis].max() fig, ax1 = plt.subplots(figsize=(16, 6)) ax1.set_title("United States") ax2 = ax1.twinx() ax3 = ax1.twinx() ax2.set(ylim=(0, y2_limit)) g1 = sns.lineplot(data = df, x = x1, y = y1, ax = ax1, color = y1_color) # plots the first set g2 = sns.lineplot(data = df, x = x1, y = y2, ax = ax2, color = y2_color) # plots the second set g3 = sns.lineplot(data = df, x = x1, y = y3, ax = ax3, color = y3_color) # plots the third set

I modified the code with your data, referring to the subgraph in the official sample.我用你的数据修改了代码,参考官方示例中的子图。 You can find the reference here .您可以在此处找到参考。

from mpl_toolkits.axes_grid1 import host_subplot
from mpl_toolkits import axisartist

# fig, ax1 = plt.subplots(figsize=(16, 6))
host = host_subplot(111, axes_class=axisartist.Axes) # update
plt.rcParams["figure.figsize"] = (16, 6) # update

ax1.set_title("United States")
# ax1 = host.twinx()
ax2 = host.twinx() # update
ax3 = host.twinx() # update

ax3.axis["right"] = ax3.new_fixed_axis(loc="right", offset=(50, 0)) # update

ax1.axis["right"].toggle(all=True) # update
ax2.axis["right"].toggle(all=True) # update

ax2.set(ylim=(0, y2_limit))
sns.lineplot(data = df, x = x1, y = y1, ax = host, color = y1_color) # plots the first set ax = ax1,
sns.lineplot(data = df, x = x1, y = y2, ax = ax2, color = y2_color) # plots the second set ax = ax2,
sns.lineplot(data = df, x = x1, y = y3, ax = ax3, color = y3_color) # plots the third set ax = ax3, 

plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 seaborn 的条形图 plot 辅助 y 轴? - How can I plot a secondary y-axis with seaborn's barplot? 如何使用条形 plot 缩放 Seaborn 的 y 轴 - How to scale Seaborn's y-axis with a bar plot 如何创建 y 轴上的值列表,而不必在 Python 中创建 plot 图形? - How can I create a list of the values on the y-axis without having to plot a graph in Python? 如何用pyplot在相同的x轴(日期时间)但不同的y轴上绘制线形图和条形图? - How to plot line and bar-chart on the same x-axis (datetime) but different y-axis with pyplot? Python Seaborn FacetGrid 不同的 y 轴 - Python Seaborn FacetGrid different y-axis 如何更改 seaborn 直方图中的 y 轴限制? - How do I change y-axis limits in seaborn histogram? 使用Seaborn将1D时间序列绘制为沿y轴具有边缘直方图的线 - Use Seaborn to plot 1D time series as a line with marginal histogram along y-axis 如何在彼此之上创建 2 个图形,它们都具有相同的 x 轴和不同的 y 轴? - How can would I create 2 graphs on top of each other with both having the same x-axis and different y-axis? 如何用 Seaborn 将 x 轴上的 plot 月和 y 轴上的降雨量? - How to plot Month on the x-axis and Rainfall on the y-axis with Seaborn? 如何在 python 中以像素数为 x 轴,灰度颜色为 y 轴 plot 图形? - How can I plot the figure with the number of pixel as a x-axis and the grayscale color as a y-axis in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM