散点图不访问y值数据(Python)

2024-04-29 02:55:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一些新鲜的眼睛。我正在尝试使用列表x和y中的值绘制散点图

尽管我有一个df.plot.scatter(“x”,“y”)的x和y值列表,但运行代码时会出现一个错误,指出

TypeError: scatter() missing 1 required positional argument: 'y'

我不知道我错过了什么。任何帮助都将不胜感激


from matplotlib import pyplot as plt
import pandas as pd
from pandas import DataFrame

  #sample lists
hole_ID = ["A", "B", "C", "D", "E", "F"]
x = [1, 2, 4, 6, 4, 6]
y = [1, 5, 3, 3, 9, 6]


  # Assign Drill_hole ID as Key, all other lists as values

dict_database = dict(zip(hole_ID, zip(x, y)))

  # Create Dataframe and assign dict_database to it

df = DataFrame
pd.DataFrame.from_dict(dict_database, orient='index', columns=["x", "y"])

  # Create and display Scatterplot

scatter1 = df.plot.scatter("x", "y")
plt.show()

Tags: fromimportiddataframepandasdf列表plot
1条回答
网友
1楼 · 发布于 2024-04-29 02:55:13

我认为你犯了一个错误,忘记了定义df:

df=pd.DataFrame.from_dict(dict_database, orient='index', columns=["x", "y"])

from matplotlib import pyplot as plt
import pandas as pd
from pandas import DataFrame

  #sample lists
hole_ID = ["A", "B", "C", "D", "E", "F"]
x = [1, 2, 4, 6, 4, 6]
y = [1, 5, 3, 3, 9, 6]


  # Assign Drill_hole ID as Key, all other lists as values

dict_database = dict(zip(hole_ID, zip(x, y)))

  # Create Dataframe and assign dict_database to it

df=pd.DataFrame.from_dict(dict_database, orient='index', columns=["x", "y"])

  # Create and display Scatterplot

scatter1 = df.plot.scatter("x", "y")
plt.show()

结果: enter image description here

相关问题 更多 >