有没有可能生成一个具有这种特定背景的图表?

2024-10-01 09:38:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要创建一个图表,它有一个网格,如下图所示。 Grid for chart 关键因素是:

  1. x轴是时间,每个记号标记30秒
  2. 图表中的y轴标签以可变间隔重复
  3. 图表必须随着数据量的增长而增长(即对于30分钟的数据,它应该是60个框宽)

我已经研究matplotlib有一段时间了,它看起来很有前途。我还设法用数据填满了图表。查看40分钟数据的结果。My progress so far

但在我投入更多时间进行研究之前,我必须知道这个目标是否可行。如果没有,我就得查其他图表。谢谢你的帮助!你知道吗

这是上面图片的来源(我的数据实际上是从csv读取的,但这里充满了随机垃圾):

from matplotlib import dates
import matplotlib.pyplot as plt
import numpy as np
import time
from datetime import datetime

my_data = list()
for i in range(3000):
    my_data.append((datetime.fromtimestamp(i + time.time()), np.random.randint(50, 200), 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
plt.ylim(50, 210)

atoco = fig.add_subplot(2, 1, 2)  # second plot
plt.ylim(0, 100)

actg.xaxis.set_minor_locator(dates.MinuteLocator())
actg.xaxis.set_major_formatter(hfmt)

atoco.xaxis.set_minor_locator(dates.MinuteLocator())
atoco.xaxis.set_major_formatter(hfmt)
plt.xticks(rotation=45)
times = []
fhr1 = []
toco = []
for key in my_data:
    times.append(key[0])
    fhr1.append(key[1])
    toco.append(key[2])
actg.plot_date(times, fhr1, '-')
atoco.plot_date(times, toco, '-')

for ax in fig.axes:
    ax.grid(True)
plt.tight_layout()
plt.show()

Tags: 数据keyimportplotmatplotlib图表figplt
1条回答
网友
1楼 · 发布于 2024-10-01 09:38:24

好吧,我想这和你想要的差不多。你知道吗

我用^{}每30秒设置一次网格(还需要确保网格设置在次要刻度上,使用ax.xaxis.grid(True,which='both')

为了重复yticklabels,我为xaxis上的每个主要记号创建一个轴的^{},并将spine移动到该记号的位置。然后我将“脊椎颜色”(spine color)设置为“无”(none),这样它就不会显示,并且会显示实际的刻度,但不会显示刻度标签。你知道吗

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()

enter image description here

相关问题 更多 >