2017-06-04 50 views
-1

我正在編寫的應用程序將日期時間和溫度觀察對的CSV數據解析到另一個城市的同一時間寫一份報告和一張散點圖。一切正常,除了散點圖顯示的是從x軸中心開始的點的垂直線,而不是x軸上每個列出的日期之上的點。matplotlib中的散點顯示圖表中心的垂直線,而不是相應的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

  • 我如何獲得matplotlib散點圖一個真正的X,Y配對圖表在這種情況下格式化?
+0

問題尋求幫助調試(「?爲什麼不是這個代碼工作」)必須包括所期望的行爲,一個特定的問題或錯誤,以及在問題本身中重現問題所需的最短代碼。沒有明確問題陳述的問題對其他讀者無益。請參閱:如何創建[mcve]。 – ImportanceOfBeingErnest

回答

0

很難在沒有您的數據的情況下向您提供您想要的東西。本質上,您正試圖在散點圖上繪製分類數據,散點圖是爲數字數據設計的。您可以使用數字數據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() 

你可以得到這樣的:我們希望,下面你要的是接近 enter image description here

+0

謝謝你的回答。這是有道理的。 – Davidt

相關問題