Тикер Matplotlib FuncFormatter - PullRequest
       0

Тикер Matplotlib FuncFormatter

0 голосов
/ 20 октября 2018

Я пытаюсь вручную установить тикер для гистограммы.Я использую функцию FunFormatter.Тем не менее, я обнаружил, что поведение FunFormmater слишком странное.Для диапазона оси X от 0 до 91 я обнаружил, что FunFormmater возвращает следующее ... Любая идея, как это работает.Вот ссылка на файл данных Заранее спасибо * *

1004 -10,0 0,0 10,0 20,0 30,0 40,0 50,0 60,0 70,0 80,0 90,0 100,0 28.805725806451605 +38,374395161290316 +41,22463709677419 47,128709677419344 48,55383064516128 49,36818548387095 51,20048387096774 52,42201612903225 53,439959677419345 +53,439959677419345 +53,03278225806451 +53,643548387096764 56,08661290322579 +59,7512096774193564.63733870967741 70.54141129032257 76.85266129032257 83.16391129032257 95.58282258064514
import numpy as np
import matplotlib.pyplot as plt
import pandas as p
import matplotlib.mlab as m
import matplotlib
import matplotlib.ticker as ticker

file1=np.load('numofdays.npz')
fig,axes=plt.subplots(ncols=1)
ax=axes
x=np.arange(len(file1['arr_0']))
y=np.array(file1['arr_0'])
ax.bar(x,y)
mydates=p.DatetimeIndex(file1['arr_1'])

def mme(xx,pos=None):
    print(xx)
#    print(mydates[int(xx-9)].strftime('%Y-%m-%d'))
    return mydates[int(xx-9)].strftime('%Y-%m-%d')

ax.xaxis.set_major_locator(ticker.MultipleLocator(10))
ax.xaxis.set_major_formatter(ticker.FuncFormatter(mme))
fig.autofmt_xdate()

1 Ответ

0 голосов
/ 20 октября 2018

Немного опасно показывать только каждую десятую метку для un данных с одинаковым интервалом, потому что вы не знаете, что происходит между ними.

Однако, чтобы запустить ваш скрипт, вам, конечно, нужно убедиться, что позиция xx является допустимым индексом массива.Например, позиция 100 недопустима, поскольку в вашем массиве всего 92 элемента.С этой целью вы можете просто ввести условие.

import numpy as np
import matplotlib.pyplot as plt
import pandas as p

import matplotlib.ticker as ticker

file1=np.load('data/numofdays.npz')

fig,ax=plt.subplots(ncols=1)

x=np.arange(len(file1['arr_0']))
y=np.array(file1['arr_0'])
ax.bar(x,y)
mydates=p.DatetimeIndex(file1['arr_1'])

def mme(xx,pos=None):
    if int(xx) in x:
        return mydates[int(xx)].strftime('%Y-%m-%d')
    else:
        return ""

ax.xaxis.set_major_locator(ticker.MultipleLocator(10))
ax.xaxis.set_major_formatter(ticker.FuncFormatter(mme))
fig.autofmt_xdate()
plt.show()

enter image description here

В качестве альтернативы, я бы определенно рассмотрел возможность составить график фактических дат.

import numpy as np
import matplotlib.pyplot as plt
import pandas as p

file1=np.load('data/numofdays.npz')

fig,ax=plt.subplots(ncols=1)

y=np.array(file1['arr_0'])

mydates = p.DatetimeIndex(file1['arr_1'])

ax.bar(mydates,y, width=60)

fig.autofmt_xdate()
plt.show()

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...