You have defined figs with the px.bar() method.

Per documentation px.bar() returns a plotly.graph_objects.Figure object.

Looking at this plotly.graph_objects.Figure class' documentation we can see all the methods available on this plotly.graph_objects.Figure class. show() appears to be a valid method for this type of object. However there is no savefig() method for this class. This is why fig.show() works and fig.savefig() doesn't work.

It looks like there is a savefig() method on the matplotlib.pyplot class as documented here, however your figs object is an instance of plotly.graph_objects.Figure not matplotlib.pyplot.

If your goal is to write your figs object to a file, it looks like the documentation specifies 3 methods that provide this functionality:

plotly.graph_objects.Figure

  • write_html
  • write_image
  • write_json

Try replacing:

figs.savefig('static/images/staff_plot.png')

with

figs.write_image(file='static/images/staff_plot.png', format='.png')

Answer from steve on Stack Overflow
🌐
freeCodeCamp
forum.freecodecamp.org › python
Attribute error: AxesSubplot' object has no attribute 'savefig' - Python - The freeCodeCamp Forum
May 14, 2021 - Tell us what’s happening: I got an error message on the Time series challenge. Could anyone kindly advise? Thanks Traceback (most recent call last): File "main.py", line 7, in time_series_visualizer.draw_bar_plot() File "/home/runner/boilerplate-page-view-time-series-visualizer-1/time_series_visualizer.py", line 43, in draw_bar_plot fig.savefig('bar_plot.png') AttributeError: 'AxesSubplot' object has no attribute 'savefig' exit status 1 My code is as below Your code so ...
Discussions

Having Problem of Pyplot Feature on Streamlit
Hello @blackary I tried subplot instead of plt.figure (…) for pie and bar chart like this and the other codes same as above: Example: plot0=plt.subplots() and st.pyplot(plot0) but I got this error: AttributeError:tuple object has no attribute savefig · Here’s how you can use st.pyplot() ... More on discuss.streamlit.io
🌐 discuss.streamlit.io
1
0
August 21, 2022
Rendering Matplotlib AxesSubplots in Streamlit
Hi everyone, I’m building a streamlit app, and am trying to make use of Seaborn plots in the application. One of my issues is that when I try and render a scatter plot using st.pyplot I get the following error message: AttributeError: 'AxesSubplot' object has no attribute 'savefig' Importantly, ... More on discuss.streamlit.io
🌐 discuss.streamlit.io
1
1
September 19, 2020
'AxesImage' object has no attribute 'savefig'
AttributeError: 'AxesImage' object has no attribute 'savefig' Traceback: File "c:\users\rcxsm\appdata\local\programs\python\python38\lib\site-packages\streamlit\script_runner.py", line 349, in _run_script exec(code, module.dict) File "C:\Users\rcxsm\Documents\phyton_scripts\streamlit_scrip... More on github.com
🌐 github.com
1
July 22, 2021
Plotting from missing library results in: AttributeError: 'AxesSubplot' object has no attribute 'savefig'
Summary When using the missingno library to visualise data sparsity within a pandas data frame an AttributeError: 'AxesSubplot' object has no attribute 'savefig' occurs. Discussed t... More on github.com
🌐 github.com
2
January 16, 2021
🌐
GitHub
github.com › matplotlib › matplotlib › issues › 12447
figure created using figure.Figure doesn't save figure · Issue #12447 · matplotlib/matplotlib
October 8, 2018 - AttributeError Traceback (most recent call last) D:\...\img_test\layou_test.py in <module>() 14 for i in range(0, 110): 15 file_name = f'{i}.png' ---> 16 figure_bug(file_name) 17 print(f'Done {i}') D:\....\img_test\layou_test.py in gridspec_bug(file) 8 axs = [fig.add_subplot(gs[x]) for x in range(1, 5)] 9 fig.text(0.5, 0.5, file, fontsize=25) ---> 10 fig.savefig(file) 11 plt.close(fig) 12 C:\....\matplotlib\figure.py in savefig(self, fname, frameon, transparent, **kwargs) 2095 self.set_frameon(frameon) 2096 -> 2097 self.canvas.print_figure(fname, **kwargs) 2098 2099 if frameon: AttributeError: 'NoneType' object has no attribute 'print_figure' Expected outcome Saved image ·
Author   abbazs
🌐
Streamlit
discuss.streamlit.io › using streamlit
Having Problem of Pyplot Feature on Streamlit - Using Streamlit - Streamlit
August 21, 2022 - Hello @blackary I tried subplot instead of plt.figure (…) for pie and bar chart like this and the other codes same as above: Example: plot0=plt.subplots() and st.pyplot(plot0) but I got this error: AttributeError:tuple object has no attribute savefig · Here’s how you can use st.pyplot() ...
🌐
GitHub
github.com › streamlit › streamlit › issues › 3593
'AxesImage' object has no attribute 'savefig' · Issue #3593 · streamlit/streamlit
July 22, 2021 - AttributeError: 'AxesImage' object has no attribute 'savefig' Traceback: File "c:\users\rcxsm\appdata\local\programs\python\python38\lib\site-packages\streamlit\script_runner.py", line 349, in _run_script exec(code, module.dict) File "C:\Users\rcxsm\Documents\phyton_scripts\streamlit_scrip...
Author   rcsmit
🌐
GitHub
github.com › streamlit › streamlit › issues › 2609
Plotting from missing library results in: AttributeError: 'AxesSubplot' object has no attribute 'savefig' · Issue #2609 · streamlit/streamlit
January 16, 2021 - Summary When using the missingno library to visualise data sparsity within a pandas data frame an AttributeError: 'AxesSubplot' object has no attribute 'savefig' occurs. Discussed this issue with @randyzwitch via email and he recommended...
Author   andymcdgeo
Find elsewhere
🌐
GitHub
github.com › facebook › prophet › issues › 169
plot_components() now returns a list, not a matplotlib Figure · Issue #169 · facebook/prophet
April 15, 2017 - >>> plot=m.plot_components(forecast) >>> plot <matplotlib.figure.Figure object at 0x10b3d4a20> while the code in Master returns a list: >>> plot=m.plot_components(forecast) >>> plot.savefig("plot-components.png") Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'list' object has no attribute 'savefig' >>> plot=m.plot_components(forecast) >>> plot [<matplotlib.lines.Line2D object at 0x115c9ebe0>, <matplotlib.collections.PolyCollection object at 0x11630b4e0>, <matplotlib.lines.Line2D object at 0x11631bba8>, <matplotlib.collections.PolyCollection object at 0x11632c630>] We should update the docs, I'm not sure how.
Author   mino98
🌐
Plotly
community.plotly.com › 📊 plotly python
Errors with displaying plotly graph objects - 📊 Plotly Python - Plotly Community Forum
March 5, 2020 - I had been running a code in Jupyter Notebook that had been working fine before. I’m using version 4.5.0 of plotly. The code added traces and output a plot, but now I see the error: AttributeError: ‘Figure’ object has no attribute ‘update_layout’ I commented out the update_layout ...
🌐
Itsourcecode
itsourcecode.com › home › ‘axessubplot’ object has no attribute ‘savefig’ [solved]
'axessubplot' object has no attribute 'savefig' [SOLVED]
March 27, 2023 - attributeerror: 'axessubplot' object has no attribute 'savefig' in Python can be easily solved by using the savefig() method on the Figure object, instead of calling it on the AxesSubplot object.
🌐
Streamlit
discuss.streamlit.io › using streamlit
'FuncAnimation' object has no attribute 'savefig' - Using Streamlit - Streamlit
April 15, 2021 - hy guys , i hope you can help me 🙂 so i am trying to show a graph on streamlit but i keep seeing this message (‘FuncAnimation’ object has no attribute ‘savefig’)whenever i rerun the app. Any idea how can i fix it?
Top answer
1 of 2
4

From the documentation for matplotlib.pyplot.hist:

The return value is a tuple (n, bins, patches) or ([n0, n1, ...], bins, [patches0, patches1,...]) if the input contains multiple data.

From the documentation for matplotlib.pyplot.savefig:

Save the current figure.

It looks like you should call savefig in the same way you call hist, not on the result of the hist call.

plt.savefig("{}_temperature.png".format(filename), format='png')
...
2 of 2
2

I've adapted your code and took the liberty to change the several lines creating a figure by list in comprehension of for loops:

import matplotlib.pyplot as plt
# should be equal when using .pylab
import numpy.random as rnd

# generate_data
n_points = 1000
temperature_graph_array = rnd.random(n_points)
feelslike_graph_array = rnd.random(n_points)
windspeed_graph_array = rnd.random(n_points)
windgustspeed_graph_array = rnd.random(n_points)
pressure_graph_array = rnd.random(n_points)
humidity_graph_array = rnd.random(n_points)
list_of_data = [temperature_graph_array,
                feelslike_graph_array,
                windspeed_graph_array,
                windgustspeed_graph_array,
                pressure_graph_array,
                humidity_graph_array]
list_of_names = ['temperature',
                 'feelslike',
                 'windspeed',
                 'windgustspeed',
                 'pressure',
                 'humidity']

# create the figures:
#figure1 = plt.figure()
#figure2 = plt.figure()
#figure3 = plt.figure()
#figure4 = plt.figure()
#figure5 = plt.figure()
#figure6 = plt.figure()
#list_of_figs = [figure1, figure2, figure3, figure4, figure5, figure6]
## could be:
list_of_figs = [plt.figure() for i in range(6)]

# create the axis:
#ax1 = figure1.add_subplot(111)
#ax2 = figure2.add_subplot(111)
#ax3 = figure3.add_subplot(111)
#ax4 = figure4.add_subplot(111)
#ax5 = figure5.add_subplot(111)
#ax6 = figure6.add_subplot(111)
#list_of_axis = [ax1, ax2, ax3, ax4, ax5, ax6]
## could be:
list_of_axis = [fig.add_subplot(111) for fig in list_of_figs]

# plot the histograms
# notice `plt.hist` returns a tuple (n, bins, patches) or
# ([n0, n1, ...], bins, [patches0, patches1,...]) if the input
# contains multiple data
#hist1 = ax1.hist(temperature_graph_array, color="blue")
#hist2 = ax2.hist(feelslike_graph_array, color="blue")
#hist3 = ax3.hist(windspeed_graph_array, color="blue")
#hist4 = ax4.hist(windgustspeed_graph_array, color="blue")
#hist5 = ax5.hist(pressure_graph_array, color="blue")
#hist6 = ax6.hist(humidity_graph_array, color="blue")
#list_of_hists = [hist1, hist2, hist3, hist4, hist5, hist6]
## could be:
list_of_hists = []
for i, ax in enumerate(list_of_axis):
    list_of_hists.append(ax.hist(list_of_data[i], color="blue"))

filename = 'output_graph'
for i, fig in enumerate(list_of_figs):
    name = list_of_names[i].capitalize()
    list_of_axis[i].set_title(name)
    fig.tight_layout()
    fig.savefig("{}_{}.png".format(filename,name), format='png')

Will not post the resulting figures, but this gives me 6 little .png files in the same folder as the script.

Even better, you can use a function to do all that to your data:

def save_hist(data, name, filename):
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.hist(data, color="blue")
    ax.set_title(name)
    fig.tight_layout()
    fig.savefig("{}_{}.png".format(filename,name), format='png')
    plt.close(fig)

filename = 'output_graph_2'
for data, name in zip(list_of_data, list_of_names):
    save_hist(data, name, filename)
🌐
PythonAnywhere
pythonanywhere.com › forums › topic › 513
Pandas plotting in IPython : Forums : PythonAnywhere
March 15, 2013 - Simply put, wherever you might normally use plt.show() to display your graph on screen you should use fig.savefig('your_graph.png') to save it as an image file instead. Once you've done that, you can view the graph from your browser using a URL like this: http://www.pythonanywhere.com/user/your-username/files/home/your-username/graph.png ''' While the object Pandas produces is a matplotlib.axes.AxesSubplot (which does not seem to have a savefig attribute) as produced by this example from Wes McKinney's 'Python for DataAnalysis':<br /> ''' Time Series Plotting Plots with pandas time series have improved date formatting compared with matplotlib out of the box.
🌐
Materials Science Community Discourse
matsci.org › pymatgen
'Figure' object has no attribute 'gcf' - pymatgen - Materials Science Community Discourse
May 23, 2022 - Hi, I am trying to write_image from PdPlotter. Whole code (below) is running okay except the last line. ######################### from pymatgen.entries.compatibility import MaterialsProjectCompatibility from pymatgen…
🌐
Seaborn
seaborn.pydata.org › generated › seaborn.FacetGrid.savefig.html
seaborn.FacetGrid.savefig — seaborn 0.13.2 documentation
Save an image of the plot · This wraps matplotlib.figure.Figure.savefig(), using bbox_inches=”tight” by default. Parameters are passed through to the matplotlib function
🌐
GitHub
github.com › matplotlib › matplotlib › issues › 7902
Saving an unpickled figure. · Issue #7902 · matplotlib/matplotlib
January 20, 2017 - Trying to save an unpickled figure using savefig throws an AttributeError: 'CallbackRegistry' object has no attribute 'callbacks' The issue is reproduced using the following code in python 2.7 (from WinPython2.7.10), Windows 8.1 (64bit),...
Author   ImportanceOfBeingErnest