首页 > 解决方案 > 散点图不访问 y 值数据(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()

标签: pythonlistdictionaryscatter-plot

解决方案


我认为您忘记定义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()

结果: 在此处输入图像描述


推荐阅读