我的 fig1.savefig 函数将我的图形保存为空白屏幕,我该如何解决?
Posted
技术标签:
【中文标题】我的 fig1.savefig 函数将我的图形保存为空白屏幕,我该如何解决?【英文标题】:My fig1.savefig function is saving my figure as a blank screen, how do I fix this? 【发布时间】:2021-01-20 21:40:55 【问题描述】:我正在通过 .csv 文件导入数据并绘制它,但是当我绘制我的图形并尝试将其保存到我的谷歌驱动器时,它只会保存一个没有任何数据的空白图像。我正在使用 Google Collaboratory。
这是我的代码:
#Displaying the Data
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.title("Score of Each Raisan Thrown", fontsize ='x-large')
plt.ylabel("Valid Raisans Thrown", fontsize='large')
plt.xlabel("Score", fontsize= 'large')
dart = data[:,0]
score = data[:,1]
plt.plot(score ,dart, marker='o', linestyle='none', color='black')
ax = plt.subplot()
ax.set_facecolor('seashell')
# Adding ticks to make my plot easier to understand/read
ax.set_yticks([20,40,60,80,100,120,140,160,180,200])
ax.set_xticks([-16,-14,-12,-10,-8,-6,-4,-2,0,2,4,6,8,10,12,14,-16])
fig = plt.figure(figsize=(10,5))
fig = plt.gcf()
plt.draw()
fig.savefig(datapath+'My Experiment1 Plot')
plt.show()
【问题讨论】:
【参考方案1】:因为你的
fig = plt.figure(figsize=(10,5))
fig = plt.gcf()
plt.draw()
fig.savefig(datapath+'My Experiment1 Plot')
出现在所有绘图命令之后,这意味着您正在创建一个新图形,而无需绘制任何内容,只需保存即可。你需要重新排列你的命令:
# get the data:
dart = data[:,0]
score = data[:,1]
# set up the fig and axes:
fig, ax = plt.subplots(figsize=(10,5))
# plot the data
plt.plot(score ,dart, marker='o', linestyle='none', color='black')
# additional formatting
plt.title("Score of Each Raisan Thrown", fontsize ='x-large')
plt.ylabel("Valid Raisans Thrown", fontsize='large')
plt.xlabel("Score", fontsize= 'large')
# even more formatting
ax.set_facecolor('seashell')
# Adding ticks to make my plot easier to understand/read
ax.set_yticks([20,40,60,80,100,120,140,160,180,200])
ax.set_xticks([-16,-14,-12,-10,-8,-6,-4,-2,0,2,4,6,8,10,12,14,-16])
fig.savefig(datapath+'My Experiment1 Plot')
plt.show()
【讨论】:
以上是关于我的 fig1.savefig 函数将我的图形保存为空白屏幕,我该如何解决?的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Python 不使用对象 .save() 将我的模型保存到数据库?
如何从同一资源运行两个函数(绘图图形并将无限数据保存到txt文件)