添加数据点时散点图中的静态色图

问题描述 投票:0回答:1

我想将数据点一个接一个地添加到屏幕的锁定部分,将所有图另存为* .png并制作一个gif。到目前为止,一切正常,但是我想锁定颜色栏,以使其在添加点期间不会更改其范围。我不知道如何解决这个问题...

输入数据是(t_string将被修改以使其起作用:]

x = [2.803480599999999, 5.5502475000000056, 6.984381300000002, 4.115224099999998, 5.746583699999995, 8.971469500000019, 12.028179500000032, 13.451193300000014, 12.457393999999972, 12.027555199999998, 16.077930800000015, 5.021229700000006, 11.206380399999999, 7.903262600000004, 11.98195070000001, 12.21701, 10.35045, 10.231890000000002]

y = [11.961321698938578, 5.218986480632915, 5.211628408660906, 4.847852635777481, 4.936266162218553, 5.233256380128127, 5.441388698929861, 5.461721129728066, 5.722170570613203, 5.2698434785261545, 5.645419662253215, 4.617062894639794, 4.973357261130752, 5.906843248930297, 5.256517482861392, 5.537361432952908, 5.339542403148332, 5.376979880224148]

t_string = ['2019-10-7', '2019-10-13', '2019-11-10', '2019-11-16', '2019-11-17', '2019-11-23', '2019-11-24', '2019-11-27', '2019-12-1', '2019-12-4', '2019-12-8', '2019-12-21', '2019-12-23', '2019-12-25', '2019-12-27', '2020-1-2', '2020-1-5', '2020-1-9']

enter image description here

下面您会找到我使用的整个代码。它将在您的工作目录中创建一个新目录并在其中写入所有文件,您还将在其中找到.gif。可能需要安装一些软件包。非常感谢广告

# Import some stuff
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

from datetime import datetime
import pathlib
from pathlib import Path
import moviepy.editor as mpy



# Define Input
x = [2.803480599999999, 5.5502475000000056, 6.984381300000002, 4.115224099999998, 5.746583699999995, 8.971469500000019,
     12.028179500000032, 13.451193300000014, 12.457393999999972, 12.027555199999998, 16.077930800000015,
     5.021229700000006, 11.206380399999999, 7.903262600000004, 11.98195070000001, 12.21701, 10.35045,
     10.231890000000002]

y = [11.961321698938578, 5.218986480632915, 5.211628408660906, 4.847852635777481, 4.936266162218553, 5.233256380128127,
     5.441388698929861, 5.461721129728066, 5.722170570613203, 5.2698434785261545, 5.645419662253215, 4.617062894639794,
     4.973357261130752, 5.906843248930297, 5.256517482861392, 5.537361432952908, 5.339542403148332, 5.376979880224148]

t_string = ['2019-10-7', '2019-10-13', '2019-11-10', '2019-11-16', '2019-11-17', '2019-11-23', '2019-11-24',
            '2019-11-27', '2019-12-1', '2019-12-4', '2019-12-8', '2019-12-21', '2019-12-23', '2019-12-25', '2019-12-27',
            '2020-1-2', '2020-1-5', '2020-1-9']

# Define a function to get the datafiles with a certain suffix in a path
def getfile_UI(file_directory, file_suffix):

    from glob import glob
    path_to_search = file_directory / file_suffix
    filenames = glob(str(path_to_search))

    return filenames


# Start of script/calculations
t = [mdates.date2num(datetime.strptime(i, "%Y-%m-%d")) for i in t_string]

workingdirectory_projectfolder = Path.cwd().parent

my_dpi = 75

for index, entry in enumerate(t_string):
    fig = plt.figure(figsize=(480 / my_dpi, 480 / my_dpi), dpi=my_dpi)
    sc = plt.scatter(x[0:index + 1],
                     y[0:index + 1],
                     c=t[0:index + 1])
    plt.xlim(0, 20)
    plt.ylim(4, 7)
    plt.title(entry)

    plt.xlabel("Distace [km]")
    plt.ylabel("Pace [min/km]")

    loc = mdates.AutoDateLocator()
    fig.colorbar(sc, ticks=loc,
                 format=mdates.AutoDateFormatter(loc))

    filename = 'png_' + str(index) + '.png'
    new_dir = workingdirectory_projectfolder /  "type 3 gif"
    pathlib.Path(new_dir).mkdir(exist_ok=True)

    plt.savefig(workingdirectory_projectfolder / "type 3 gif" / filename, dpi=96)
    plt.gca()

# Make a GIF from all png files
#       http://superfluoussextant.com/making-gifs-with-python.html
fps = 10
gif_name = str(fps) + " fps_""type3_"
workingdirectory_projectfolder = Path.cwd().parent
gif_path = workingdirectory_projectfolder / "type 3 gif" / gif_name

filenamelist_path = workingdirectory_projectfolder / "type 3 gif"
filenamelist_png = getfile_UI(filenamelist_path, "*.png")

list.sort(filenamelist_png, key=lambda x: int(
    x.split('_')[1].split('.png')[0]))  # Sort the images by #, this may need to be tweaked for your use case

clip = mpy.ImageSequenceClip(filenamelist_png, fps=fps)
clip.write_gif('{}.gif'.format(gif_path), fps=fps)
python-3.x matplotlib gif colormap
1个回答
0
投票

您可以用plt.scatter呼叫vmin=min(t), vmax=max(t)。这修复了用于着色的限制。

您可以在动画中添加的其他功能是仅显示当前的刻度日期:

loc = mdates.AutoDateLocator()
cbar = fig.colorbar(sc, ticks=loc, format=mdates.AutoDateFormatter(loc))
ticks = [ti for ti in cbar.get_ticks() if ti <= t[index]]
cbar.set_ticks(ticks)
© www.soinside.com 2019 - 2024. All rights reserved.