简体   繁体   English

我的 fig1.savefig 函数将我的图形保存为空白屏幕,我该如何解决这个问题?

[英]My fig1.savefig function is saving my figure as a blank screen, how do I fix this?

I am importing data by a .csv file and am plotting it, but when I plot my figure and try to save it to my google drive it only saves a blank image with none of the data.我正在通过 .csv 文件导入数据并绘制它,但是当我绘制图形并尝试将其保存到我的谷歌驱动器时,它只会保存一个没有任何数据的空白图像。 I am using Google Collaboratory.我正在使用 Google Collaboratory。

Here is my code:这是我的代码:

#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()


Because your因为你的

fig = plt.figure(figsize=(10,5))

fig = plt.gcf()
plt.draw()
fig.savefig(datapath+'My Experiment1 Plot')

comes after all the drawing commands, which means you are creating a new figure without plotting anything and just save it.在所有绘图命令之后出现,这意味着您正在创建一个新图形而无需绘制任何内容并保存它。 You need to rearrange your commands:您需要重新排列命令:

# 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM