簡體   English   中英

如何讓x軸只顯示我想繪制的數據? 而不是包括沒有情節的標簽?

[英]How do i get the x axis to only show my data i want to plot? Instead of it including labels with no plots?

我正在繪制日期與整數。 但是因為日期范圍相隔100天,所以matplotlib會自動包含100天之間的每個日期,即使只有20個左右的數據也可以繪制。 所以我的問題是,如何讓x軸只生成有數據繪制的日期? 此外,目前日期是以橫向方式壓扁的,我如何以垂直方式獲取它們以便更適合?

這是我的圖表的圖片: 在此輸入圖像描述

這是我的代碼:

    import datetime as dt
    import matplotlib.dates as mdates

    array1 = ['2014-10-28', '2014-11-17', '2014-09-29', '2014-10-17', '2014-10-22']
    array2 = [1,4,5,6,9]

    x = [dt.datetime.strptime(a,'%Y-%m-%d').date() for a in array1]    
    plt.plot_date((x), (array2), 'ro')
    plt.show() 

好的,首先,您可以使用fig.autofmt_xdate()函數自動處理xtick標簽 - 這是最簡單的方法。

所以你會有這樣的事情:

import datetime as dt
import matplotlib.dates as mdates
from matplotlib import pyplot
from random import randint


array1 = ['2014-10-28', '2014-11-17', '2014-09-29', '2014-10-17', '2014-10-22']
array2 = [1,4,5,6,9]


dates = ["2014-{month:0>2d}-{day:0>2d}".format(month=m, day=d) for m in [1,5] for d in range(1,32)]
dates = [dt.datetime.strptime(d, '%Y-%m-%d').date() for d in dates]
freqs = [randint(0,4) for _ in dates]

fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.plot_date(dates, freqs, "ro")

fig.autofmt_xdate()

pyplot.show()

在此輸入圖像描述

仍然有你不想要的大差距,但刻度標簽更好。

接下來,為了處理拆分,有一些選項,但它們不在主matplotlib(我知道)。 這是通過實際繪制兩個圖,在中間刪除刺,並使用sharey選項完成的:

import datetime as dt
import matplotlib.dates as mdates
from matplotlib import pyplot
from random import randint
from matplotlib.ticker import MaxNLocator


dates = ["2014-{month:0>2d}-{day:0>2d}".format(month=m, day=d) for m in [1,5] for d in range(1,10)]
dates = [dt.datetime.strptime(d, '%Y-%m-%d').date() for d in dates]
freqs = [randint(0,4) for _ in dates]



fig = pyplot.figure()

ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2, sharey=ax1)

ax1.plot_date(dates, freqs, "ro")
ax2.plot_date(dates, freqs, "ro")


#set the upper and lower bounds for the two adjacent plots
ax1.set_xlim(xmax=dt.datetime.strptime("2014-01-11", '%Y-%m-%d').date())
ax2.set_xlim(xmin=dt.datetime.strptime("2014-05-01", '%Y-%m-%d').date())


for ax in [ax1, ax2]:
  _ = ax.get_xticklabels() #For some reason, if i don't do this, then it only prints years for the tick labels. :/
  ax.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=6))

#Turn off the spines in the middle
ax1.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)

ax1.yaxis.tick_left()

ax2.get_yaxis().set_visible(False)

pyplot.subplots_adjust(wspace=0.1)

fig.autofmt_xdate()

pyplot.show()

還有一些額外的東西需要清理,但我想你明白了。

在此輸入圖像描述

您可以旋轉xtick標簽:

plt.xticks(rotation=45)

並使用以下方法在x軸上均勻繪制日期:

x = np.arange(n)
ax.plot(x, array2, 'ro')

然后重新標記xticks:

locs = x
labels = [d.strftime('%b %d') for d in dates]
plt.xticks(locs, labels)

但請注意,手動設置位置和標簽的副作用是刻度將不再像使用GUI平移或縮放工具時通常那樣自動適應刻度的變化。


import time
import datetime as dt
import numpy as np
import matplotlib.dates as mdates
import matplotlib.pyplot as plt

n, m = 20, 30
now = dt.date.today()
dates = [now+dt.timedelta(days=i) for i in range(n//2)+range(m,m+n//2)]
x = np.arange(n)
array2 = np.random.random(n)

fig, ax = plt.subplots()
ax.plot(x, array2, 'ro')

locs = x
labels = [d.strftime('%b %d') for d in dates]
plt.xticks(locs, labels)
plt.xticks(rotation=45)
plt.show() 

產量

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM