从Djangohtmlvi获取反转图像

2024-04-19 22:26:02 发布

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

我正在尝试在Django上制作基于web的snake&;ladder游戏,为此我使用matplotlibmpld3在网页上创建和显示我的游戏数据。你知道吗

我可以在网页上得到我的图像,但它是反向的。你知道吗

我在这个代码中哪里出错了?你知道吗

我的视图.py文件:

import matplotlib.image as mpimg
import mpld3 as mpld3
import numpy as np
import matplotlib.pyplot as plt

def home(request):
    image =  mpimg.imread('platform3.png')
    figure = plt.figure(dpi=100)
    subplot = figure.add_subplot(111)
    subplot.set_xlim(0, 10)
    subplot.set_ylim(0, 10)
    subplot.imshow(image, extent=[0, 10, 0, 10])
    canvas = FigureCanvas(figure)
    response = django.http.HttpResponse(content_type='image/png')
    canvas.print_png(response)
    html_gen = 'graph.html'
    with open(html_gen, 'w') as f:
        f.write(mpld3.fig_to_html(figure))
        f.write("Hello")

    return render(request, html_gen)

我应该提供生成的html吗_生成.html文件也一样?你知道吗


Tags: 文件imageimport游戏网页pngmatplotlibhtml
1条回答
网友
1楼 · 发布于 2024-04-19 22:26:02

只需将[0, 0]放在轴的左下角:

subplot.imshow(image, extent=[0, 10, 0, 10], origin='lower')

请看一下^{} docs

更新:如何检查和设置默认image.origin

  1. 支票:

    >>> import matplotlib as mpl
    >>> print(mpl.rcParams['image.origin'])
    upper
    
  2. lower设置为默认值(docs):

    $ echo "image.origin : lower" >> .config/matplotlib/matplotlibrc
    
  3. 再次检查:

    >>> import matplotlib as mpl
    >>> print(mpl.rcParams['image.origin'])
    upper
    

相关问题 更多 >