2

I need to draw graphs in python matplotlib.

For each graph I do some calculates before and then draw the graphe. Here is some of the code:

import matplotlib.pyplot as plt

def draw(LuxCoordinates, finalPix, verName):
    plt.axes([0.2, 0.2, 0.7, 0.6]);
    plt.xlim(0,3500); #Axis x
    plt.ylim(0,100); #Axis y
    plt.plot(LuxCoordinates, finalPix, 'g');
    plt.scatter(LuxCoordinates, finalPix, linewidths=1)
    plt.grid(axis)
    plt.xlabel('X_Coordinates', color='r');
    plt.ylabel('Y_Coordinates', color='r');
    plt.title(verName, color='#afeeee');
    savefig(verName+'.png');
    plt.show();

My problem is that I call to this function twice or more, according to the number of graphs I have, I get the graphs in separate plots, but I want to draw all graphs on the same plot, in order to compare them. How can I do it? Thank you all!

1 Answer 1

4

remove plt.show()

your function should get a figure or axis as an input parameter, and do the plotting on that figure, and then outside the function when you have done all the plotting you may call plt.show()

here is a minimal example:

import matplotlib.pyplot as plt
import numpy as np

def plotter ( ax, col ):
    data = np.random.normal( size=(50, 2 ) )
    x, y = data[:, 0], data[:, 1 ]
    ax.scatter( x, y, color=col )

fig = plt.figure()
ax = fig.add_axes([0.2, 0.2, 0.7, 0.6])

plotter( ax, 'Blue' )
plotter( ax, 'Red' )
fig.show( )
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, I just remove plt.show() and put it after all calls to the draw function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.