鱼C论坛

 找回密码
 立即注册
查看: 1156|回复: 1

[已解决]matplotlib的x轴

[复制链接]
发表于 2020-8-4 14:12:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
a = [66.5, 70.19, 67.35, 67.35, 67.38, 63.9, 61.14, 62.98, 61.84, 63.25, 63.4]
b = [14.39, 15.08 ,16.97, 17.58, 18.05, 18.17, 18.66, 18.81, 20.44, 20.24 ,20.35]
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这种
有什么方法吗?求大神解答
最佳答案
2020-8-4 14:14:15
本帖最后由 xiaosi4081 于 2020-8-4 14:24 编辑
import matplotlib.pyplot as plt

a = [66.5, 70.19, 67.35, 67.35, 67.38, 63.9, 61.14, 62.98, 61.84, 63.25, 63.4]
b = [14.39, 15.08 ,16.97, 17.58, 18.05, 18.17, 18.66, 18.81, 20.44, 20.24 ,20.35]
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()
QQ截图20200804140534.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-8-4 14:14:15 | 显示全部楼层    本楼为最佳答案   

回帖奖励 +2 鱼币

本帖最后由 xiaosi4081 于 2020-8-4 14:24 编辑
import matplotlib.pyplot as plt

a = [66.5, 70.19, 67.35, 67.35, 67.38, 63.9, 61.14, 62.98, 61.84, 63.25, 63.4]
b = [14.39, 15.08 ,16.97, 17.58, 18.05, 18.17, 18.66, 18.81, 20.44, 20.24 ,20.35]
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()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-19 14:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表