Python 有没有可能生成一个具有这种特定背景的图表?
我需要创建一个图表,它有一个网格,如下图所示。 关键因素包括:Python 有没有可能生成一个具有这种特定背景的图表?,python,matplotlib,charts,Python,Matplotlib,Charts,我需要创建一个图表,它有一个网格,如下图所示。 关键因素包括: x轴是时间轴,每个刻度标记30秒 图表中的y轴标签以可变间隔重复 图表必须随着数据量的增长而增长(即,对于30分钟的数据,图表的宽度应为60框) 我已经研究matplotlib一段时间了,它看起来很有希望。我还设法用数据填充了图表。40分钟的数据见我的结果 但在我投入更多时间进行研究之前,我必须知道这个目标是否可能实现。如果不是的话,我将不得不查看其他图表。谢谢你的帮助 以下是上述图像的来源(my_数据实际上是从csv读取的,但此处
好吧,我想这是你想要的东西 我曾经每30秒设置一次网格(还需要确保网格设置在小刻度上,使用
ax.xaxis.grid(True,which='tware')
要重复yticklabels
,我为X轴上的每个主刻度创建一个轴,并将移动到该刻度的位置。然后,我将脊椎颜色设置为“无”,使其不显示,并旋转实际刻度,但不旋转刻度标签
from matplotlib import dates
import matplotlib.pyplot as plt
import numpy as np
import time
from datetime import datetime
# how often to show xticklabels and repeat yticklabels:
xtickinterval = 10
# Make random data
my_data = list()
for i in range(3000):
my_data.append((datetime.fromtimestamp(i + time.time()), np.random.randint(120, 160), np.random.randint(10, 100)))
hfmt = dates.DateFormatter('%H:%M:%S')
fig = plt.figure()
actg = fig.add_subplot(2, 1, 1) # two rows, one column, first plot
actg.set_ylim(50, 210)
atoco = fig.add_subplot(2, 1, 2,sharex=actg) # second plot, share the xaxis with actg
atoco.set_ylim(-5, 105)
# Set the major ticks to the intervals specified above.
actg.xaxis.set_major_locator(dates.MinuteLocator(byminute=np.arange(0,60,xtickinterval)))
# Set the minor ticks to every 30 seconds
minloc = dates.SecondLocator(bysecond=[0,30])
minloc.MAXTICKS = 3000
actg.xaxis.set_minor_locator(minloc)
# Use the formatter specified above
actg.xaxis.set_major_formatter(hfmt)
times = []
fhr1 = []
toco = []
for key in my_data:
times.append(key[0])
fhr1.append(key[1])
toco.append(key[2])
print times[-1]-times[0]
# Make your plot
actg.plot_date(times, fhr1, '-')
atoco.plot_date(times, toco, '-')
for ax in [actg,atoco]:
# Turn off the yticklabels on the right hand side
ax.set_yticklabels([])
# Set the grids
ax.xaxis.grid(True,which='both',color='r')
ax.yaxis.grid(True,which='major',color='r')
# Create new yticklabels every major tick on the xaxis
for tick in ax.get_xticks():
tx = ax.twinx()
tx.set_ylim(ax.get_ylim())
tx.spines['right'].set_position(('data',tick))
tx.spines['right'].set_color('None')
for tic in tx.yaxis.get_major_ticks():
tic.tick1On = tic.tick2On = False
plt.tight_layout()
plt.show()
你能分享你的代码吗?@tom:刚刚这么做,谢谢你的耐心。哇,看起来真的很好!这么接近我需要的东西,我被吓坏了。:D现在我要深入研究matplotlib,我必须给网格上色(绿色或红色,用户选择)和其他东西。但这是我看到的主要障碍。谢谢!啊,对不起。快速跟进。有300秒的数据,这很好。但我通常会遇到更多…至少是10倍。3000秒多一点,我得到以下运行时错误:RRuleLocator估计从2016-02-23 15:21:41.5生成3049个滴答81725+00:00至2016-02-23 16:12:31.099945+00:00:00:超过定位器MAXTICKS*2(2000)---你对此有什么想法或建议吗?你可以设置次要记号定位器的
MAXTICKS
参数。默认值为1000。我通过将其设置为1500发现,似乎可以工作3000秒。在我上面编辑的示例中,我将其设置为3000以给你一些摆动空间。(我还为你将网格更改为红色)
from matplotlib import dates
import matplotlib.pyplot as plt
import numpy as np
import time
from datetime import datetime
# how often to show xticklabels and repeat yticklabels:
xtickinterval = 10
# Make random data
my_data = list()
for i in range(3000):
my_data.append((datetime.fromtimestamp(i + time.time()), np.random.randint(120, 160), np.random.randint(10, 100)))
hfmt = dates.DateFormatter('%H:%M:%S')
fig = plt.figure()
actg = fig.add_subplot(2, 1, 1) # two rows, one column, first plot
actg.set_ylim(50, 210)
atoco = fig.add_subplot(2, 1, 2,sharex=actg) # second plot, share the xaxis with actg
atoco.set_ylim(-5, 105)
# Set the major ticks to the intervals specified above.
actg.xaxis.set_major_locator(dates.MinuteLocator(byminute=np.arange(0,60,xtickinterval)))
# Set the minor ticks to every 30 seconds
minloc = dates.SecondLocator(bysecond=[0,30])
minloc.MAXTICKS = 3000
actg.xaxis.set_minor_locator(minloc)
# Use the formatter specified above
actg.xaxis.set_major_formatter(hfmt)
times = []
fhr1 = []
toco = []
for key in my_data:
times.append(key[0])
fhr1.append(key[1])
toco.append(key[2])
print times[-1]-times[0]
# Make your plot
actg.plot_date(times, fhr1, '-')
atoco.plot_date(times, toco, '-')
for ax in [actg,atoco]:
# Turn off the yticklabels on the right hand side
ax.set_yticklabels([])
# Set the grids
ax.xaxis.grid(True,which='both',color='r')
ax.yaxis.grid(True,which='major',color='r')
# Create new yticklabels every major tick on the xaxis
for tick in ax.get_xticks():
tx = ax.twinx()
tx.set_ylim(ax.get_ylim())
tx.spines['right'].set_position(('data',tick))
tx.spines['right'].set_color('None')
for tic in tx.yaxis.get_major_ticks():
tic.tick1On = tic.tick2On = False
plt.tight_layout()
plt.show()