0

I am new to Python and below is an extract of some Python codes in my Jupyter Notebook:

import pandas as pd
import matplotlib.pyplot as plt

import numpy as np
import seaborn as sns
sns.set(style="darkgrid")
FacetGrid.set(yticks=np.arange(0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500, 8000))
import numpy as np
%matplotlib inline

plt.rcParams['figure.figsize']=(20,20)

When I run these codes, I get the following error: NameError: name 'FacetGrid' is not defined

I did a search and found the following: Change number of x-axis ticks in seaborn plots

However, I cannot seem to be able to implement it correctly in my codes.I need the FacetGrid function to manually specify the ticks values on the y-axis of a seaborn stripplot.

How do I correct for this?

Community
  • 1
  • 1
user3115933
  • 4,303
  • 15
  • 54
  • 94
  • Why do you think `FacetGrid` is in-scope? I don't see it imported from anything in the code you've posted. (There is no `from whatever import FacetGrid`, for example.) – Kevin J. Chase Mar 19 '17 at 10:16
  • Ok I get your point, Digging deeper into the thing, Facetgrid takes 4 arguments and therefore, my code about setting the tick marks are definitely not in the correct place. Basically, I want my y-axis to show those values instead of the default ones of Seaborn stripplot. Any idea or pointers on how to proceed. – user3115933 Mar 19 '17 at 10:40

1 Answers1

1

The main point is that you need to work with an instance of FacetGrid not the class itself. Also, you need to define your numpy array for the ticklabels correctly.

Here is an example based on your code:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="darkgrid")

data = pd.DataFrame({})
g = sns.FacetGrid(data, size=20)

yticks = np.arange(0,8500,500)
g.set(yticks=yticks)

plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thanks a lot! It works but I lose the parameter of the figure size: (plt.rcParams['figure.figsize']=(20,20) There is something that seems to override that line although Python does not throw any error message. – user3115933 Mar 22 '17 at 06:33
  • 1
    For some obscure but intended reason, the FacetGrid does not respect the figure size set by the rcParams or with the `.set` function. Those obscure reasons are detailed by @mwaskom in this [related issue](https://github.com/mwaskom/seaborn/issues/444). Instead, the figure size shall be determinded by the FacetGrid's `size` argument. See [this answer](http://stackoverflow.com/a/28765059/4124317) for how it's meant to be used. I edited the answer accordingly. – ImportanceOfBeingErnest Mar 22 '17 at 07:50