Carla
Carla

Reputation: 3380

Python draw graph with seaborn

I'd need to perform some business intelligence on a set of data. I can see that python has a rich set of libraries such as Seaborn so I'd like to give it a go. I'm trying to run this example from the showcase:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

sns.set_theme(style="ticks")

# Create a dataset with many short random walks
rs = np.random.RandomState(4)
pos = rs.randint(-1, 2, (20, 5)).cumsum(axis=1)
pos -= pos[:, 0, np.newaxis]
step = np.tile(range(5), 20)
walk = np.repeat(range(20), 5)
df = pd.DataFrame(np.c_[pos.flat, step, walk],
                  columns=["position", "step", "walk"])

# Initialize a grid of plots with an Axes for each walk
grid = sns.FacetGrid(df, col="walk", hue="walk", palette="tab20c",
                     col_wrap=4, height=1.5)

# Draw a horizontal line to show the starting point
grid.refline(y=0, linestyle=":")

# Draw a line plot to show the trajectory of each random walk
grid.map(plt.plot, "step", "position", marker="o")

# Adjust the tick positions and labels
grid.set(xticks=np.arange(5), yticks=[-3, 3],
         xlim=(-.5, 4.5), ylim=(-3.5, 3.5))

# Adjust the arrangement of the plots
grid.fig.tight_layout(w_pad=1)

I've installed the required libs, however when I run the python file I simply get a:

/usr/bin/python3 seaplot.py
"Process finished with exit code 0" 

Nothing else shows up. I have just a basic knowledge of Python so I wonder, do I need to set something on my env to display graphics in Python or maybe add a waiting thread...? any idea?

Upvotes: 0

Views: 334

Answers (1)

Helios
Helios

Reputation: 715

I have just tried it out and it worked so I will make my comment an answer:

Add plt.show() to the end of your script, this command is needed to tell matplotlib to actually render the image. Up until that point you may add more lines or modify attributed etc, show() will render the image.

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.show.html

Upvotes: 1

Related Questions