Pandas无法在x和y轴上绘制相同的数据。

huangapple go评论72阅读模式
英文:

Pandas can not plot the same data on both x and y

问题

我想在x轴上绘制y_1,并在y轴上绘制y_1,以获得理想的线,但出现错误。

以下是我的代码,

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

def plot_me():
       df = pd.DataFrame({
        'y_1': [20, 30, 290, 675, 800],
        'y_2': [22, 25, 281, 600, 900]
        }, index=[1990, 1997, 2003, 2009, 2014])
   
    df.plot.line(x='y_1', y='y_2')    
    df.plot.line(x='y_1', y='y_1')
    plt.show()

plot_me()

然而,如果我更改为,

 df.plot.scatter(x='y_1', y='y_1')

它就可以工作,也可以这样工作。

英文:

I want to plot y_1 on x and y_1 on y axis for being ideal line but there is an error.

The following is my code,

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

def plot_me():
       df = pd.DataFrame({
        'y_1': [20, 30, 290, 675, 800],
        'y_2': [22, 25, 281, 600, 900]
        }, index=[1990, 1997, 2003, 2009, 2014])
   
    df.plot.line(x='y_1', y='y_2')    
    df.plot.line(x='y_1', y='y_1')
    plt.show()

plot_me()

File "/home/MyUser/.local/lib/python3.9/site-packages/pandas/core/indexes/base.py", line 3623, in get_loc raise KeyError(key) from err

However, this works if I change to,
df.plot.scatter(x='y_1', y='y_1')

df.plot.scatter(x='y_1', y='y_1')
and it works

答案1

得分: 0

这是因为df.plot.line()不接受相同的输入参数。根据文档:

x:标签或位置,可选
允许绘制一列与另一列的图表。如果未指定,则使用DataFrame的索引。

y:标签或位置,可选
允许绘制一列与另一列的图表。如果未指定,则使用所有数字列。

关键词在于另一列。相比之下,df.plot.scatter()没有提到这一点:

x:整数或字符串
要用作每个点的水平坐标的列名或列位置。

编辑:根据您的评论,将列复制到新列中是可行的:

def plot_me():
    df = pd.DataFrame({
        'y_1': [20, 30, 290, 675, 800],
        'y_2': [22, 25, 281, 600, 900]
    }, index=[1990, 1997, 2003, 2009, 2014])
    df['y_test'] = df['y_1']
    df.plot.line(x='y_1', y='y_2')    
    df.plot.line(x='y_1', y='y_test')
    plt.show()

plot_me()

显示:

Pandas无法在x和y轴上绘制相同的数据。

英文:

This is because of df.plot.line() doesn't accept the same input for both parameters. As per the docs:

> x: label or position, optional
Allows plotting of one column versus another. If not specified, the index of the DataFrame is used.

> y: label or position, optional
Allows plotting of one column versus another. If not specified, all numerical columns are used.

With the key word here being another. In contrast, df.plot.scatter() doesn't mention this:

>x: int or str
The column name or column position to be used as horizontal coordinates for each point.

EDIT: Based on your comment, copying the column into a new one, does work:

def plot_me():
       df = pd.DataFrame({
        'y_1': [20, 30, 290, 675, 800],
        'y_2': [22, 25, 281, 600, 900]
        }, index=[1990, 1997, 2003, 2009, 2014])
       df['y_test'] = df['y_1']
       df.plot.line(x='y_1', y='y_2')    
       df.plot.line(x='y_1', y='y_test')
       plt.show()

plot_me()

Showing:

Pandas无法在x和y轴上绘制相同的数据。

答案2

得分: 0

Pandas的绘图功能不支持直接在x轴和y轴上同时绘制相同的数据。这种限制的原因是,Pandas的绘图方法主要设计用于可视化DataFrame中不同列或变量之间的关系。

如果您想要在x轴和y轴上绘制相同的数据,可以直接使用matplotlib来实现。以下是如何修改代码以使用matplotlib在两个轴上绘制相同数据的示例:

import pandas as pd
import matplotlib.pyplot as plt

def plot_me():
    df = pd.DataFrame({
        'y_1': [20, 30, 290, 675, 800],
        'y_2': [22, 25, 281, 600, 900]
    }, index=[1990, 1997, 2003, 2009, 2014])

    fig, ax = plt.subplots()
    ax.plot(df['y_1'], df['y_1'], label='y_1')
    ax.set_xlabel('y_1')
    ax.set_ylabel('y_1')
    ax.legend()
    plt.show()

plot_me()
英文:

Pandas' plotting functionality does not support plotting the same data on both the x-axis and y-axis directly. The reason for this limitation is that pandas' plotting methods are primarily designed for visualizing relationships between different columns or variables in a DataFrame.

If you want to plot the same data on both the x-axis and y-axis, you can use matplotlib directly to achieve this. Here's an example of how you can modify the code to plot the same data on both axes using matplotlib:

import pandas as pd
import matplotlib.pyplot as plt

def plot_me():
    df = pd.DataFrame({
        'y_1': [20, 30, 290, 675, 800],
        'y_2': [22, 25, 281, 600, 900]
    }, index=[1990, 1997, 2003, 2009, 2014])

    fig, ax = plt.subplots()
    ax.plot(df['y_1'], df['y_1'], label='y_1')
    ax.set_xlabel('y_1')
    ax.set_ylabel('y_1')
    ax.legend()
    plt.show()

plot_me()




</details>



huangapple
  • 本文由 发表于 2023年6月5日 20:19:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定