【问题标题】:My fig1.savefig function is saving my figure as a blank screen, how do I fix this?我的 fig1.savefig 函数将我的图形保存为空白屏幕,我该如何解决?
【发布时间】: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()


【问题讨论】:

    标签: python python-3.x numpy matplotlib google-colaboratory


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多