python - 即使在 plt.plot 之前使用 plt.figure () 也会出现错误 <Figure size 1000x600 with 1 Axes>

标签 python python-3.x

这是我的代码中出现错误的部分(都与图表相关,但要点:

plt.figure (figsize = (10,6))
plt.title ("Alfa x CL")
plt.plot (Alpha,CL, "b",linewidth = 2, label = "Xflr5")
plt.plot (alfa,cl, "r",linewidth = 2, label = "Experimental")
plt.legend (loc = 'upper left')
plt.grid (True)
plt.xlabel ("Alfa")
plt.ylabel ("Cl")
plt.savefig (grafico01) #grafico01 is a variable used before
plt.show ()

它显示消息

而不是图形(再次运行程序后,由于某种原因显示图形)

经过大量搜索后,我相信错误在于我放置函数的顺序,但我不知道哪个是正确的,我在这里找到的所有内容都是关于 plt.figure ()plt.plot ()之后(这里的情况并非如此)...订单有帮助吗?还是别的什么?

最佳答案

像已有的那样将变量放入绘图函数中。尝试使用ax直接在所需的轴上绘图。这是首选的做事方式:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(10,6))
ax.plot(range(0,10),range(10,0,-1),  'b',linewidth = 2, label = 'Xflr5')
ax.plot(range(0,10),[i*(i**(1/2)) for i in range(0,10)], 'r',linewidth = 2, label = 'Experimental')
ax.set_title('Alfa x CL')
ax.legend(loc='upper left')
ax.grid(True)
ax.set_xlabel ("Alfa")
ax.set_ylabel ("Cl")
plt.savefig ('grafico01.png') #grafico01 is a variable used before
plt.show ()

对于另一个数字,只需执行:

fig2, ax2 = plt.subplots(figsize=(10,6))
ax2.plot(range(0,10),range(10,0,-1),  'b',linewidth = 2, label = 'Xflr5')

就像以前一样。

enter image description here

关于python - 即使在 plt.plot 之前使用 plt.figure () 也会出现错误 <Figure size 1000x600 with 1 Axes>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55053886/

相关文章:

python-3.x - 带有 Flask、sqlalchemy 和 sqlite 的 Openshift 应用程序 - 数据库恢复问题

python - "sorted 1-d iterator"基于 "2-d iterator"(迭代器的笛卡尔积)

python - 如何为 python 2.7 安装 pip?

python - 使用 Python api 客户端在 Google Cloud Storage Bucket 中提取 zip 存档

python - Django prefetch_related 优化查询但仍然很慢

python - 经过快速启动后,在访问 hadoop 时遇到一些问题

python - 无法将从 MongoDB 检索到的 json 值返回到 python flask 中的 HTML 页面

python - 嵌套列表操作

python - 为什么容器的最大尺寸有符号位?

python-3.x - 如何对标签或按钮进行永久性条目更改