Home > database >  Saving multiple different pyplot figures results in empty graph
Saving multiple different pyplot figures results in empty graph

Time:12-04

The following code should save two different plots in 'file1.png' and 'file2.png'. The first plot is saved correctly, however, the second file displays a blank graph, with incorrect values for the axis.

    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates

    plt.plot(dates, values_1)
    plt.grid(axis = 'y', color = 'gray', linestyle = '-', linewidth = 0.5)
    plt.xticks(rotation=45, fontweight='light',  fontsize='x-small')
    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y'))
    plt.savefig("file1.png", dpi = 300, bbox_inches='tight')
    plt.close()

    plt.plot(dates, values_2)
    plt.grid(axis = 'y', color = 'gray', linestyle = '-', linewidth = 0.5)
    plt.xticks(rotation=45, fontweight='light',  fontsize='x-small')
    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y'))
    plt.savefig("file2.png", dpi = 300,bbox_inches='tight') 
    plt.close()

I have tried almost all possible combinations of plt.close(), plt.close('all'), plt.figure(), plt.cla() and plt.clf() and still have the same problem.

CodePudding user response:

The code you provided should save two different plots in 'file1.png' and 'file2.png'. However, the second plot is saved with incorrect values for the axis because you are using the plt.gca() method to get the current axes and set the x-axis formatter, but you have already closed the first figure using the plt.close() method.

This means that when you call the plt.gca() method in the second part of your code, you are getting the current axes of a new, empty figure, and not the axes of the second plot. This is why the second plot is saved with incorrect values for the x-axis.

To fix this issue, you can move the code that sets the x-axis formatter and saves the figure to a separate function, and call this function twice to save the two plots. This way, you can avoid closing the first figure and the second plot will be saved with the correct values for the x-axis.

Here is an example of how you could modify your code to save the two plots correctly:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates

def save_plot(dates, values, filename):
    plt.plot(dates, values)
    plt.grid(axis = 'y', color = 'gray', linestyle = '-', linewidth = 0.5)
    plt.xticks(rotation=45, fontweight='light',  fontsize='x-small')
    plt.gca().x

EDIT:

Yes, that is correct -- the plt.gca().xaxis.set_major_formatter() function call needs to be included in both plt.plot() functions in order to properly format the x-axis of both plots.

It looks like you are still seeing the same issue with the second plot, where it is a replica of the first plot instead of showing the expected values. There could be a few reasons for this.

One possible issue is that the dates and values_1 and values_2 variables are not being updated between the two plt.plot() function calls, so both plots are using the same data. You can check this by printing the values of these variables before and after the first plt.plot() function call, to see if they have been updated correctly.

Another possible issue is that the plt.plot() function is not being called correctly in the second plot. You can check the documentation for this function to make sure that you are using the correct syntax and arguments.

Finally, you may be encountering a bug in matplotlib that is causing the second plot to be a replica of the first plot. If this is the case, you can try using a different version of matplotlib, or filing a bug report with the matplotlib team to help them investigate and fix the issue.

  • Related