如何在 python 中的两个不同 y 轴上绘制两个变量?
How to plot two variables on two different y-axes in python?
我尝试构建一个具有一个 x 轴和两个 y 轴的图形,所有轴都来自数据框(类似于图形 here)。这是我的示例数据:
import pandas as pd
df = pd.DataFrame(data={'year': [2000, 2001, 2002],
'deaths': [327, 456, 509],
'cheese': [13.5, 13.7, 13.8]})
我只找到了涵盖 or more than two axis with an answer I, as a beginner, don't understand.
的问题
matplotlib.pyplot
模块创建图形和坐标轴对象(详见 help(plt.subplots)
),可用于按要求创建绘图:
import matplotlib.pyplot as plt # Impot the relevant module
fig, ax = plt.subplots() # Create the figure and axes object
# Plot the first x and y axes:
df.plot(x = 'year', y = 'deaths', ax = ax)
# Plot the second x and y axes. By secondary_y = True a second y-axis is requested:
# (see https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html for details)
df.plot(x = 'year', y = 'cheese', ax = ax, secondary_y = True)
输出:
我尝试构建一个具有一个 x 轴和两个 y 轴的图形,所有轴都来自数据框(类似于图形 here)。这是我的示例数据:
import pandas as pd
df = pd.DataFrame(data={'year': [2000, 2001, 2002],
'deaths': [327, 456, 509],
'cheese': [13.5, 13.7, 13.8]})
我只找到了涵盖
matplotlib.pyplot
模块创建图形和坐标轴对象(详见 help(plt.subplots)
),可用于按要求创建绘图:
import matplotlib.pyplot as plt # Impot the relevant module
fig, ax = plt.subplots() # Create the figure and axes object
# Plot the first x and y axes:
df.plot(x = 'year', y = 'deaths', ax = ax)
# Plot the second x and y axes. By secondary_y = True a second y-axis is requested:
# (see https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html for details)
df.plot(x = 'year', y = 'cheese', ax = ax, secondary_y = True)
输出: