我有一個腳本,它使用matplotlib的plot_date()
函數將查詢結果轉換爲時間序列可視化。它曾經運作良好,但在某些時候(不確定爲什麼)日期開始表現出色。Matplotlib plot_date x軸標籤關閉了幾天,部分丟失
這裏的底層數據:
0 = {list} <type 'list'>: [u'2017-07-03', 427]
1 = {list} <type 'list'>: [u'2017-07-10', 201]
2 = {list} <type 'list'>: [u'2017-07-17', 594]
3 = {list} <type 'list'>: [u'2017-07-24', 525]
4 = {list} <type 'list'>: [u'2017-07-31', 239]
5 = {list} <type 'list'>: [u'2017-08-07', 437]
下面的代碼生成圖形:
def generate_timeseries(values, **kwargs):
x_values = []
y_values = []
for item in values['rows']:
x_values.append(datetime.strptime(item[0], "%Y-%m-%d"))
y_values.append(item[1])
# General Figure
figure = plt.figure()
low = min(y_values)
high = max(y_values)
plt.ylim([0, math.ceil(high + 0.4 * (high - low))])
figure.set_size_inches(kwargs['chart_size'][0], kwargs['chart_size'][1], forward=True)
plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
# General Axis
axis = figure.add_subplot(1, 1, 1)
axis.spines["top"].set_visible(False)
axis.spines["right"].set_visible(False)
# Y-Axis
axis.get_yaxis().tick_left()
axis.yaxis.label.set_color('gray')
axis.yaxis.grid()
if kwargs['show_axis_labels']:
axis.set_ylabel(kwargs['y_label'])
# X-Axis
axis.get_xaxis().tick_bottom()
axis.xaxis.label.set_color('gray')
if kwargs['show_axis_labels']:
axis.set_xlabel(kwargs['x_label'])
axis.xaxis.set_major_formatter(DateFormatter(kwargs.get('date_format', '%Y-%m-%d')))
plt.plot_date(x_values,
y_values,
linestyle='-',
color=kwargs['palette'][0],
marker='o' if kwargs['marker'] else None,
linewidth=1.6,
clip_on=False)
if kwargs['show_datapoint_labels']:
for xy_comb in zip(x_values, y_values):
axis.annotate('{}'.format(xy_comb[1]), xy_comb,
xycoords='data',
xytext=(2, 10),
textcoords='offset points')
if kwargs['show_legend']:
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), fancybox=True, fontsize=6)
return plt
請注意:
- 有6個數據點,但只有5日期標籤可見
- 似乎是
我已經驗證了plot_date()
功能之前輸入的日期5天的轉變;我已經驗證了數據的時區(這不能解釋爲期5天的轉變,但仍然)。我嘗試過基於here建議的格式化和操作日期的不同變化,但似乎沒有任何方法可以解決問題。
有沒有人遇到類似的問題,或者可以在我的代碼中發現問題?我花了幾個小時試圖調試/研究,覺得我迄今沒有得到任何地方。
[Matplotlib V2.0.2; Python 2.7.13]
究竟什麼是* *的問題?你展示的情節似乎是完全正確的,標籤的位置也是正確的。如果你想將你的標籤放在不同的位置,你需要確切地知道你想要的位置。 – ImportanceOfBeingErnest
正如我最初寫的 - 我錯過了1個標籤,其他人沒有與數據點對齊(沒有到位,也沒有日期) –
所以你想在每個數據點的位置有一個標籤,並且沒有在沒有被數據點佔據的位置上的標籤? (我想這很明顯,這個要求並不是默認的,因爲如果繪製了200個點,那麼它會生成200個標籤,而且肯定是不希望的。) – ImportanceOfBeingErnest