matplotlib 中的散点图在图表中心显示垂直点线,而不是在相应的 X 轴值上方

Scatter in matplotlib shows a vertical line of dots in the center of the chart instead of above the corresponding X axis values

我正在编写的应用程序将日期时间和温度观测对的 CSV 数据解析为另一个城市的同一时间,然后编写报告和散点图。一切正常,除了散点图显示从 x 轴中心开始的垂直点线,而不是 x 轴上每个列出日期上方的点。

这是我应用 matplotlib 的代码。

import matplotlib.pyplot as pp
.
.
.
# Code that imports the CSV, changes the times to the other city ....
.
.
.

paris_stamps = [] # This list is a list of datetimes that compose the X axis

i = 0
while i < len(parsedObservations):
  paris_stamps.append(parsedObservations[i][1])
  i += 1

observed_values = [] # these are the temperatures that go on the X axis
i = 0
while i < len(parsedObservations): 
  observed_values.append(parsedObservations[i][0])
  i += 1

# the code below is the interaction with matplotlib 

paris_stamps = [pandas.to_datetime(d) for d in paris_stamps] # sanitize the string datetimes to a format matplotlib will accept

pp.scatter(x = paris_stamps,y = observed_values, s = 500, c='blue')
pp.show()

当我 运行 这个的时候,我得到这个图表:Chart with a vertical line of dots instead of a horizontal series of dots above each of the x axis values

如果没有您的数据,很难准确地提供您想要的东西。本质上,您是在尝试在专为数字数据设计的散点图上绘制分类数据。您可以使用数值数据 range(0, len(observed_values)) 先完成绘图。然后您可以根据需要将刻度标签更改为相应的类别。希望以下内容接近您想要的内容:

from matplotlib import pyplot as plt

observed_values = [6, 3, 1, 5, 2, 4]
paris_stamps = ['2017-04', '2017-05', '2017-06', '2017-07', '2017-08', '2017-09']
plt.scatter(range(0, len(observed_values)), observed_values)
plt.xticks(range(0, len(observed_values)), paris_stamps)
plt.show()

你可以得到这个: