2017-07-07 62 views
0

爲了各種信息與同步的x軸(時間軸,numpy.datetime64)結合我已經寫了下面的例子:Matplotlib,同步散射 - ,violin-之間時間軸和箱線圖

import numpy as np 
import matplotlib.pyplot as plt 

x_axis = [np.datetime64("2016-01-01"), np.datetime64("2016-02-01")] 
y_data = [3, 5] 

fig = plt.figure() 
ax1 = fig.add_subplot(311) 
ax2 = fig.add_subplot(312, sharex=ax1) 
ax3 = fig.add_subplot(313, sharex=ax1) 

# Plot the scatter plot  
ax1.scatter(x_axis, y_data) 

# Now draw the violinplot and the boxplot 
for (timestamp, y) in zip(x_axis, y_data): 
    data = np.random.rand(100) + y 
    ax2.violinplot(data, 
        positions=[timestamp], 
        widths=[np.timedelta64(500000, 's')]) 
# ax3.boxplot(data, 
#    positions=[timestamp], 
#    widths=[np.timedelta64(10, 's')]) 

問題現在是第三個(註釋)部分:當定義位置和withs以正確的單位時,一切正常,因此散點圖可以繪製在時間軸上,但boxplot在繪製晶須時會失敗。我收到一個TypeError: ufunc multiply cannot use operands with types dtype('float64') and dtype('<M8[D]')

有沒有什麼方法可以在實時軸上正確繪製箱形圖,以便同步按需要工作?

PS:注意,寬度都在單位秒,如果時間單位是太大,violinplot不順暢任何更多...

回答

0

你應該看看seaborn這一點。 violinplot()有多個選項可以包含箱線圖等。

+0

我試圖使用seaborn,但獲得時間軸的正常工作並不像預期的那樣真正工作,特別是在嘗試與其他matplotlib數字同步軸時。 – Sosel

相關問題