1

I have a function that plots a graph. I can call this graph with different variables to alter the graph. I'd like to call this function multiple times and plot the graphs along side each other but not sure how to do so

def plt_graph(x, graph_title, horiz_label):
    df[x].plot(kind='barh')
    plt.title(graph_title)
    plt.ylabel("")
    plt.xlabel(horiz_label)

plt_graph('gross','Total value','Gross (in millions)')
2
  • What are library are you using to plot. Could you provide an example of how your function looks like. If it is a function that accepts you to pass axes, you can always generate how you want your figure to look like with fig, ax = plt.subplots(1, 2) and then pass your axes to the function appropriately. Commented Oct 22, 2016 at 15:37
  • hopefully my edit offers better clarification. using pandas and matplotlib.pyplot but would also be useful to know how to implement in seaborn as well. Thanks. Commented Oct 22, 2016 at 16:04

3 Answers 3

1

In case you know the number of plots you want to produce beforehands, you can first create as many subplots as you need

fig, axes = plt.subplots(nrows=1, ncols=5)

(in this case 5) and then provide the axes to the function

def plt_graph(x, graph_title, horiz_label, ax):
    df[x].plot(kind='barh', ax=ax)

Finally, call every plot like this

plt_graph("framekey", "Some title", "some label", axes[4])

(where 4 stands for the fifth and last plot)

Sign up to request clarification or add additional context in comments.

4 Comments

Okey, this doesn't seem to work in matplotlib 3.0 How can I see the acceptable arguments to plot?
@wpkzz "This" works fine (and it's not depending on the matplotlib version at all). But the question may be a bit what "this" is, given that OP hasn't provided any complete code. So the assumption here is that df is a pandas Dataframe with at least one column named "framekey". You may refer to the other answer for a more complete code. And in general, if you cannot solve your problem, ask a new question, with a complete problem description.
"this" that I am refering to, is the ax argument in plot. It simply refuses, by giving a very long error. But I seemed to solve it by using it as a sort of conditional argument and then doing argumentaxis.plot( etc etc) inside the function.
df[x].plot is a pandas function, while ax.plot is a matplotlib function.
0

I have created a tool to do this really easily. I use it all the time in jupyter notebooks and find it so much neater than a big column of charts. Copy the Gridplot class from this file:

https://github.com/simonm3/analysis/blob/master/analysis/plot.py

Usage:

gridplot = Gridplot()
plt.plot(x)
plt.plot(y)

It shows each new plot in a grid with 4 plots to a row. You can change the size of the charts or the number per row. It works for plt.plot, plt.bar, plt.hist and plt.scatter. However it does require you use matplot directly rather than pandas.

If you want to turn it off:

gridplot.off()

If you want to reset the grid to position 1:

gridplot.on()

Comments

0

Here is a way that you can do it. First you create the figure which will contain the axes object. With those axes you have something like a canvas, which is where every graph will be drawn.

fig, ax = plt.subplots(1,2)

Here I have created one figure with two axes. This is a one row and two columns figure. If you inspect the ax variable you will see two objects. This is what we'll use for all the plotting. Now, going back to the function, let's start with a simple dataset and define the function.

df = pd.DataFrame({"a": np.random.random(10), "b": np.random.random(10)})
def plt_graph(x, graph_title, horiz_label, ax):
    df[x].plot(kind = 'barh', ax = ax)
    ax.set_xlabel(horiz_label)
    ax.set_title(graph_title)

Then, to call the function you will simply do this:

plt_graph("a", "a", "a", ax=ax[0])
plt_graph("b", "b", "b", ax=ax[1])

Note that you pass each graph that you want to create to any of the axes you have. In this case, as you have two, you pass the first to the first axes and so on. Note that if you include seaborn in your import (import seaborn as sns), automatically the seaborn style will be applied to your graphs.

This is how your graphs will look like.

enter image description here

When you are creating plotting functions, you want to look at matplotlib's object oriented interface.

Comments

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.