matplotlib的x轴
a =b =
c = ['2020-07-12', '2020-07-13', '2020-07-14', '2020-07-15', '2020-07-16', '2020-07-17', '2020-07-18', '2020-07-19', '2020-07-20', '2020-07-21', '2020-07-22']
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(c, a, 'g-')
ax2.plot(c, b, 'b-')
ax1.set_xlabel('X data')
ax1.set_ylabel('Y1,color=g',fontsize=15)
ax2.set_ylabel('Y2,color=b',fontsize=15)
ax1.set_xticklabels(c, rotation=45)
plt.show()
假设x轴的数值很多,比如是2020-01-01到2020-07-22,那x轴那肯定会显示的很重复,有什么办法解决这个问题嘛
比如这个例子,我想要每天日期之间存在2天间隔,x轴显示的标记是['2020-07-12', '2020-07-15', , '2020-07-18', '2020-07-20',, '2020-07-22'],类似tick_spacing = 2这种
有什么方法吗?求大神解答 本帖最后由 xiaosi4081 于 2020-8-4 14:24 编辑
import matplotlib.pyplot as plt
a =
b =
c = ['2020-07-12', '2020-07-13', '2020-07-14', '2020-07-15', '2020-07-16', '2020-07-17', '2020-07-18', '2020-07-19', '2020-07-20', '2020-07-21', '2020-07-22']
new_a = []
for index,i in enumerate(a):
if index % 3 == 0:
new_a.append(i)
new_b = []
for index,i in enumerate(b):
if index % 3 == 0:
new_b.append(i)
new_c = []
for index,i in enumerate(c):
if index % 3 == 0:
new_c.append(i)
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(new_c, new_a, 'g-')
ax2.plot(new_c, new_b, 'b-')
ax1.set_xlabel('X data')
ax1.set_ylabel('Y1,color=g',fontsize=15)
ax2.set_ylabel('Y2,color=b',fontsize=15)
ax1.set_xticklabels(new_c, rotation=45)
plt.show()
页:
[1]