Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中绘制非连续的自定义日期格式_Python_Pandas_Matplotlib - Fatal编程技术网

如何在python中绘制非连续的自定义日期格式

如何在python中绘制非连续的自定义日期格式,python,pandas,matplotlib,Python,Pandas,Matplotlib,我有一个自定义日期格式的列:YYYYWeekNo 当我有从201752跳到201801的数据时,问题就出现了,这会创建一个图,将数据视为100个刻度的连续数据,因此图形是扭曲的 有没有办法用非连续的x标签解析此绘图 x = [] year = 2017 for i in range(104): j = i+1 if j <= 52: x.append(year * 100 + j) else: k = j - 52 x

我有一个自定义日期格式的列:
YYYYWeekNo

当我有从201752跳到201801的数据时,问题就出现了,这会创建一个图,将数据视为100个刻度的连续数据,因此图形是扭曲的

有没有办法用非连续的
x
标签解析此绘图

x = []
year = 2017
for i in range(104):
    j = i+1
    if j <= 52:
        x.append(year * 100 + j)
    else:
        k = j - 52
        x.append(((year+1) * 100 ) + k)


np.random.seed(1)
values = np.random.randint(low=0, high=5000, size=104)


df = pd.DataFrame.from_dict({'year_week': x, 'values': values})
df.set_index('year_week').plot(figsize=(15, 5))
x=[]
年份=2017年
对于范围(104)内的i:
j=i+1

如果j可以使用“断开”轴。这将是定制版本:

import matplotlib.pylab as plt
import numpy as np

x = []
year = 2017
for i in range(104):
    j = i+1
    if j <= 52:
        x.append(year * 100 + j)
    else:
        k = j - 52
        x.append(((year+1) * 100 ) + k)
np.random.seed(1)
values = np.random.randint(low=0, high=5000, size=104)

df = pd.DataFrame.from_dict({'year_week': x, 'values': values})

f,(ax,ax2) = plt.subplots(1,2,sharey=True, facecolor='w', figsize=(10,10))

# plot the same data on both axes
ax.plot(df['year_week'], df['values'])
ax2.plot(df['year_week'], df['values'])

ax.set_xlim(201700,201753)
ax2.set_xlim(201800,201853)

# hide the spines between ax and ax2
ax.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax.yaxis.tick_left()
ax.tick_params()
ax2.yaxis.tick_right()

#this gets rid of scientific notation, so it's more readable
ax.ticklabel_format(useOffset=False)
ax2.ticklabel_format(useOffset=False)
plt.show()
将matplotlib.pylab作为plt导入
将numpy作为np导入
x=[]
年份=2017年
对于范围(104)内的i:
j=i+1
如果j