Python 在同一轴上创建两个标签并填充间隙
从这个问题中获得一些指导,在底部创建我的条形图; 但我似乎无法解决的问题是条形图的空白区域。正如您在代码中看到的,列表Python 在同一轴上创建两个标签并填充间隙,python,matplotlib,Python,Matplotlib,从这个问题中获得一些指导,在底部创建我的条形图; 但我似乎无法解决的问题是条形图的空白区域。正如您在代码中看到的,列表y3的长度为length=21,对于x、y1、y2也是如此。空的天数是周末,但在创建任何绘图之前,这些天数将从列表中删除。我看不出它们是如何以及为什么会留下间隙的,即使我已经将它们从我的x轴列表中删除了 编辑:我缩短了问题 您的x轴没有周末的数据 在下面的代码中,您创建了xs,但不包括周末 x=[] 对于范围(0,29)内的i: 如果((d+timedelta(days=i)
y3
的长度为length=21
,对于x
、y1
、y2
也是如此。空的天数是周末,但在创建任何绘图之前,这些天数将从列表中删除。我看不出它们是如何以及为什么会留下间隙的,即使我已经将它们从我的x轴列表中删除了
编辑:我缩短了问题
您的x轴没有周末的数据
在下面的代码中,您创建了x
s,但不包括周末
x=[]
对于范围(0,29)内的i:
如果((d+timedelta(days=i)).date().weekday()>4):
持续
x、 追加((d+timedelta(days=i)).date())
例如,如果二月一日是星期五,那么x
数组的第一个元素是[0,3,4,5]
。您没有包括周末的数据,但是通过省略[1,2]
,您就留下了他们占用的空间
解决方案:删除周末的地点并处理标签格式
您应该做的是创建没有间隙的代理x轴数据,然后重新处理标签。完整的解决方案可能如下所示
from datetime import datetime,timedelta
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.ticker as ticker
d = datetime.strptime('2019-02-01','%Y-%m-%d')
#create x-axis which consists of all days in february except weekdays, plus the first day in march
x = []
dates = {}
day = 0
for i in range(0,29):
if((d + timedelta(days=i)).date().weekday() > 4):
continue
x.append(day)
dates[day] = (d + timedelta(days=i)).date()
day += 1
def format_date(x, pos=None):
return dates[int(x)].strftime('%d-%B')
y1 = [317.92, 319.1, 322.73, 324.22, 322.05, 321.06, 323.79, 325.29, 325.55, 327.9, 329.99, 330.92, 331.33, 332.75, 331.41, 333.22, 332.58, 333.64, 331.19, 329.83, 332.46]
y2 = [-0.377507469, -0.3411700881, 0.055641283900000005, 0.44570105590000003, 0.10930618490000005, -0.2948038151, 0.09190098490000004, 0.12858223490000004, 0.09559385990000004, 0.4806987649, 0.11333709890000004, 0.5247366639000001, 0.11605396190000006, -0.30122159309999996, -0.7405398680999999, -0.7053236033999999, -0.33368165239999986, -0.8210733183999999, -1.2753887724, -1.3106836659999999, -1.3450585547999998]
y3 = [27, 15, 12, 4, 5, 11, 4, 14, 18, 19, 13, 17, 28, 22, 4, 15, 26, 21, 21, 21, 9]
fig, ax1 = plt.subplots()
ax1.plot(x, y1, 'b')
ax1.tick_params('y', colors='b')
#create space at the bottom for the bar-plot
pad = 0.25
yl = ax1.get_ylim()
ax1.set_ylim(yl[0]-(yl[1]-yl[0])*pad,yl[1])
fig.suptitle('Some title', fontsize=16)
ax2 = ax1.twinx()
ax2.plot(x, y2,'r')
ax2.tick_params('y', colors='r')
#create space at the bottom for the bar-plot
pad = 0.25
yl = ax2.get_ylim()
ax2.set_ylim(yl[0]-(yl[1]-yl[0])*pad,yl[1])
#create barplot and make it small by adding a (in this case, this is not a modular solution) max-limit
ax3 = ax1.twinx()
ax3.set_ylim(0,250)
ax3.bar(x, y3, color='green', width=1, alpha=0.5)
ax3.set_xlim(min(x),max(x))
ax3.get_yaxis().set_ticks([])
ax1.set_ylabel('1st Value', color='b')
ax2.set_ylabel('2nd Value', color='r')
plt.gcf().autofmt_xdate()
plt.gca().xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
plt.show()
这将生成图像
这是相同的数据,但没有周末的差距
差异的解释
这次练习我改变了三个方面。首先,我将x
设置为最终表示数组[0,1,2,…,20]
,并创建了一个字典dates
,以跟踪x[I]
表示的日期
x=[]
日期={}
日=0
对于范围(0,29)内的i:
如果((d+timedelta(days=i)).date().weekday()>4):
持续
x、 追加(天)
日期[天]=(d+timedelta(天=i)).date()
天数+=1
然后,我创建了一个函数format\u date
,用于馈送到matplotlib.ticker
,以处理x轴的标签
def格式_日期(x,pos=None):
返回日期[int(x)]。strftime(“%d-%B”)
最后,我将x轴设置为使用此格式函数。这将使用必须导入的matplotlib.ticker
#在导入中,添加
将matplotlib.ticker导入为ticker
#描述x轴时,请使用
plt.gca().xaxis.set\u major\u格式化程序(ticker.FuncFormatter(format\u date))
如果您有3个问题,我建议您问3个问题。对于第一个问题,为什么存在周末间隙:这与绘制plt时相同。绘图([0,1,2,5,6,7],…])
,根本没有[3,4,5]的数据。查看matplotlib文档。是的,很抱歉,我认为这是一个更好的主意,因为3个问题只会导致90%的问题被复制(因为它们是小问题,但相互关联)。关于您发送的链接,我已经尝试使用其中的部分内容,但无法将其与我的代码一起使用。谢谢你的提示,先生,你真是太棒了!
from datetime import datetime,timedelta
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.ticker as ticker
d = datetime.strptime('2019-02-01','%Y-%m-%d')
#create x-axis which consists of all days in february except weekdays, plus the first day in march
x = []
dates = {}
day = 0
for i in range(0,29):
if((d + timedelta(days=i)).date().weekday() > 4):
continue
x.append(day)
dates[day] = (d + timedelta(days=i)).date()
day += 1
def format_date(x, pos=None):
return dates[int(x)].strftime('%d-%B')
y1 = [317.92, 319.1, 322.73, 324.22, 322.05, 321.06, 323.79, 325.29, 325.55, 327.9, 329.99, 330.92, 331.33, 332.75, 331.41, 333.22, 332.58, 333.64, 331.19, 329.83, 332.46]
y2 = [-0.377507469, -0.3411700881, 0.055641283900000005, 0.44570105590000003, 0.10930618490000005, -0.2948038151, 0.09190098490000004, 0.12858223490000004, 0.09559385990000004, 0.4806987649, 0.11333709890000004, 0.5247366639000001, 0.11605396190000006, -0.30122159309999996, -0.7405398680999999, -0.7053236033999999, -0.33368165239999986, -0.8210733183999999, -1.2753887724, -1.3106836659999999, -1.3450585547999998]
y3 = [27, 15, 12, 4, 5, 11, 4, 14, 18, 19, 13, 17, 28, 22, 4, 15, 26, 21, 21, 21, 9]
fig, ax1 = plt.subplots()
ax1.plot(x, y1, 'b')
ax1.tick_params('y', colors='b')
#create space at the bottom for the bar-plot
pad = 0.25
yl = ax1.get_ylim()
ax1.set_ylim(yl[0]-(yl[1]-yl[0])*pad,yl[1])
fig.suptitle('Some title', fontsize=16)
ax2 = ax1.twinx()
ax2.plot(x, y2,'r')
ax2.tick_params('y', colors='r')
#create space at the bottom for the bar-plot
pad = 0.25
yl = ax2.get_ylim()
ax2.set_ylim(yl[0]-(yl[1]-yl[0])*pad,yl[1])
#create barplot and make it small by adding a (in this case, this is not a modular solution) max-limit
ax3 = ax1.twinx()
ax3.set_ylim(0,250)
ax3.bar(x, y3, color='green', width=1, alpha=0.5)
ax3.set_xlim(min(x),max(x))
ax3.get_yaxis().set_ticks([])
ax1.set_ylabel('1st Value', color='b')
ax2.set_ylabel('2nd Value', color='r')
plt.gcf().autofmt_xdate()
plt.gca().xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
plt.show()