2

This is my data and the graph I want to produce:

from numpy import array
from os.path import expanduser
import matplotlib.pyplot as plt
sns.set(style="darkgrid")

data = [array([8, 4, 9, 6, 2]), array([5, 4, 4, 1, 2]), array([4, 3, 1, 5, 6]), array([8, 3, 5, 6, 4])]

for d in data:
    plt.plot(d)

# plt.savefig(expanduser("~/Documents/rural_juror.pdf"))

enter image description here

Since I want to plot several of those in the same seaborn facetgrid I cannot use a loop, but have to do it on one line, like so:

plt.plot(*data)
# produces same result as 
# plt.plot(data[0], data[1], data[2], data[3])

This does not work, however, but produces a graph like so:

enter image description here

I'm guessing this has to do with the arguments to plot being of the type data, looks, data, looks... but how do I work around this?

3 Answers 3

4

Yes, you need to give the x and y values if you want to do it with a one-liner. You could do it like this:

import numpy as np
x = np.arange(5)
data = [x, array([8, 4, 9, 6, 2]),
        x, array([5, 4, 4, 1, 2]),
        x, array([4, 3, 1, 5, 6]),
        x, array([8, 3, 5, 6, 4])]
plt.plot(*data)
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, it is x and y, not x and aes. Thanks! The relevant docs are here: matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot Had I found the earlier I would not have asked. Thanks again.
1

For a one line loop statement you can use a list comprehension like this:

lines = [plt.plot(d) for d in data]

lines will be a list of matplotlib.lines.Line2D objects.

Comments

0

I can't test this without installing seaborn, but it looks like the FacetGrid uses a DataFrame for is data input. http://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.FacetGrid.html

So this should work

import pandas as pd
data = [array([8, 4, 9, 6, 2]), array([5, 4, 4, 1, 2]), array([4, 3, 1, 5, 6]), array([8, 3, 5, 6, 4])]

df = pd.DataFrame(data)
df.transpose() #not sure if you need it transposed
g = sns.FacetGrid(df)

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.