以下是我正在编写的代码的精简版本,以制作带有多个子图的图形.问题出现在最后一行,fig.savefig("foo" + ".pdf", format='pdf').我收到以下错误:
AttributeError: "'NoneType' object has no attribute 'print_figure'"
Run Code Online (Sandbox Code Playgroud)
谁能告诉我什么是错的?谢谢!
import matplotlib.pyplot as plt
from pdb import set_trace as st
import numpy as np
Time = np.array([0,1,2,3,4])
Load = np.array([1,2,3,4,5])
Cell = np.array([6,9,2,5,4])
axialLoadDueToCellPressure = 5.5 * Cell
volumePlotFormat = 'double-wide'
fig = plt.Figure()
ax1 = plt.subplot2grid((3,2), (0,0))
ax1.plot((Time/1000/60),
(Load + axialLoadDueToCellPressure)/6, 'k-')
ax1.plot((Time/1000/60), Cell, 'k--')
st()
fig.savefig("foo" + ".pdf", format='pdf')
Run Code Online (Sandbox Code Playgroud) 我有一个简单的项目,它需要一长串三元组作为输入(描述 3 维路径的坐标),并生成一个 STL 文件作为输出。(STL 是一种文件格式,用于描述用于 CAD 和 3-D 打印应用程序的3-D 表面几何形状。)
我是一名科学家,而不是一名开发人员,而且我能够使用 Python,因此我可以在需要时处理输入文本文件的解析。我正在寻找的是命令行实用程序、库或简单的编程语言,它们可以自动执行一些简单的 CAD 任务(例如,沿路径拉伸横截面),从而为 STL 输出创建真正的 3-D 表面。
谢谢。
我希望以下代码确定子图的大小,以使生成的PDF宽度为5英寸,高度为8英寸。但是,无论我插入什么figsize,生成的文件都是8英寸宽和6英寸高。我究竟做错了什么?
import matplotlib.pyplot as plt
import matplotlib.gridspec as gs
fig = plt.Figure(figsize=(5,8))
fig.set_canvas(plt.gcf().canvas)
gs1 = gs.GridSpec(3,2)
gs1.update(wspace=0.4,hspace=0.4)
ax1 = plt.subplot(gs1[0,0])
ax2 = plt.subplot(gs1[0,1])
ax3 = plt.subplot(gs1[1,0])
ax4 = plt.subplot(gs1[1,1])
ax5 = plt.subplot(gs1[2,:])
ax1.plot([1,2,3],[4,5,6], 'k-')
fig.savefig("foo.pdf", format='pdf')
Run Code Online (Sandbox Code Playgroud)
糟糕,编辑过后,我也曾尝试过fig.set_size_inches((5,8)),但这似乎也没有任何效果。
我是Python新手,并尝试使用PIL执行Arduino项目所需的解析任务.这个问题涉及Image.convert()调色板,抖动等的方法和选项.
我有一次能够显示只有16色图像的一些硬件(但他们可以指定RGB三胞胎).因此,我想自动执行任意色彩PNG图像的任务,选择"最佳"16色调色板来表示它,并将图像转换为仅包含16种颜色的调色图像.
我想用抖动.问题是,这种image.convert()方法看起来有点时髦.它的参数没有完整记录(Image.convert()的PIL文档)所以我不知道这是我的错还是该方法是错误的.
我的代码的简单版本如下:
import Image
MyImageTrueColor = Image.new('RGB',100,100) # or whatever dimension...
# I paste some images from several other PNG files in using MyImageTrueColor.paste()
MyImageDithered = MyImageTrueColor.convert(mode='P',
colors=16,
dither=1
)
Run Code Online (Sandbox Code Playgroud)
根据我做的一些搜索(例如:如何用PIL减少调色板)我认为这种方法应该做我想要的,但没有运气.它会抖动图像,但会产生超过16种颜色的图像.
为了确保,我删除了"抖动"参数.相同的输出.
我重新添加了"dither = 1"参数并抛出了Image.ADAPTIVE参数(如上面的链接所示),只是为了看看发生了什么.这导致图像包含16种颜色,但没有抖动.
我在这里错过了什么吗?是PIL车吗?我想出的解决方案是执行两个步骤,但这似乎是草率和不必要的.我想弄清楚如何做到这一点:-)为了完整性,这里是我的代码版本,产生正确的结果 - 但它以一种草率的方式做到了.(第一步产生带有> 16种颜色的抖动图像,第二步产生仅包含16种颜色的图像.)
MyImage_intermediate = MyImageTrueColor.convert(mode='P',
colors=16
)
MyImageDithered = MyImage_intermediate.convert(mode='P',
colors=16,
dither=1,
palette=Image.ADAPTIVE
)
Run Code Online (Sandbox Code Playgroud)
谢谢!