While doing some practice problems using seaborn and a Jupyter notebook, I realized that the distplot() graphs did not have the darker outlines on the individual bins that all of the sample graphs in the documentation have. I tried creating the graphs using Pycharm and noticed the same thing. Thinking it was a seaborn problem, I tried some hist() charts using matplotlib, only to get the same results.
import matplotlib.pyplot as plt
import seaborn as sns
titanic = sns.load_dataset('titanic')
plt.hist(titanic['fare'], bins=30)
yielded the following graph:
Finally I stumbled across the 'edgecolor' parameter on the plt.hist() function, and setting it to black did the trick. Unfortunately I haven't found a similar parameter to use on the seaborn distplot() function, so I am still unable to get a chart that looks like it should.
I looked into changing the rcParams in matplotlib, but I have no experience with that and the following script I ran seemed to do nothing:
import matplotlib as mpl
mpl.rcParams['lines.linewidth'] = 1
mpl.rcParams['lines.color'] = 'black'
mpl.rcParams['patch.linewidth'] = 1
mpl.rcParams['patch.edgecolor'] = 'black'
mpl.rcParams['axes.linewidth'] = 1
mpl.rcParams['axes.edgecolor'] = 'black'
I was just kind of guessing at the value I was supposed to change, but running my graphs again showed no changes.
I then attempted to go back to the default settings using mpl.rcdefaults()
but once again, no change.
I reinstalled matplotlib using conda but still the graphs look the same. I am running out of ideas on how to change the default edge color for these charts. I am running the latest versions of Python, matplotlib, and seaborn using the Conda build.
As part of the update to matplotlib 2.0 the edges on bar plots are turned off by default. However, you may use the rcParam
plt.rcParams["patch.force_edgecolor"] = True
to turn the edges on globally.
Probably the easiest option is to specifically set the edgecolor when creating a seaborn plot, using the hist_kws argument,
ax = sns.distplot(x, hist_kws=dict(edgecolor="k", linewidth=2))
For matplotlib plots, you can directly use the edgecolor or ec argument.
plt.bar(x,y, edgecolor="k")
plt.hist(x, edgecolor="k")
Equally, for pandas plots,
df.plot(kind='hist',edgecolor="k")
A complete seaborn example:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
x = np.random.randn(100)
ax = sns.distplot(x, hist_kws=dict(edgecolor="k", linewidth=2))
plt.show()
As of Mar, 2021 :
sns.histplot(data, edgecolor='k', linewidth=2)
work.
Using hist_kws=dict(edgecolor="k", linewidth=2) gave an error:
AttributeError: 'PolyCollection' object has no property 'hist_kws'
Using the available styles in seaborn could also solve your problem.
Available styles in seaborn are :
ticks
dark
darkgrid
white
whitegrid
Related
While doing some practice problems using seaborn and a Jupyter notebook, I realized that the distplot() graphs did not have the darker outlines on the individual bins that all of the sample graphs in the documentation have. I tried creating the graphs using Pycharm and noticed the same thing. Thinking it was a seaborn problem, I tried some hist() charts using matplotlib, only to get the same results.
import matplotlib.pyplot as plt
import seaborn as sns
titanic = sns.load_dataset('titanic')
plt.hist(titanic['fare'], bins=30)
yielded the following graph:
Finally I stumbled across the 'edgecolor' parameter on the plt.hist() function, and setting it to black did the trick. Unfortunately I haven't found a similar parameter to use on the seaborn distplot() function, so I am still unable to get a chart that looks like it should.
I looked into changing the rcParams in matplotlib, but I have no experience with that and the following script I ran seemed to do nothing:
import matplotlib as mpl
mpl.rcParams['lines.linewidth'] = 1
mpl.rcParams['lines.color'] = 'black'
mpl.rcParams['patch.linewidth'] = 1
mpl.rcParams['patch.edgecolor'] = 'black'
mpl.rcParams['axes.linewidth'] = 1
mpl.rcParams['axes.edgecolor'] = 'black'
I was just kind of guessing at the value I was supposed to change, but running my graphs again showed no changes.
I then attempted to go back to the default settings using mpl.rcdefaults()
but once again, no change.
I reinstalled matplotlib using conda but still the graphs look the same. I am running out of ideas on how to change the default edge color for these charts. I am running the latest versions of Python, matplotlib, and seaborn using the Conda build.
As part of the update to matplotlib 2.0 the edges on bar plots are turned off by default. However, you may use the rcParam
plt.rcParams["patch.force_edgecolor"] = True
to turn the edges on globally.
Probably the easiest option is to specifically set the edgecolor when creating a seaborn plot, using the hist_kws argument,
ax = sns.distplot(x, hist_kws=dict(edgecolor="k", linewidth=2))
For matplotlib plots, you can directly use the edgecolor or ec argument.
plt.bar(x,y, edgecolor="k")
plt.hist(x, edgecolor="k")
Equally, for pandas plots,
df.plot(kind='hist',edgecolor="k")
A complete seaborn example:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
x = np.random.randn(100)
ax = sns.distplot(x, hist_kws=dict(edgecolor="k", linewidth=2))
plt.show()
As of Mar, 2021 :
sns.histplot(data, edgecolor='k', linewidth=2)
work.
Using hist_kws=dict(edgecolor="k", linewidth=2) gave an error:
AttributeError: 'PolyCollection' object has no property 'hist_kws'
Using the available styles in seaborn could also solve your problem.
Available styles in seaborn are :
ticks
dark
darkgrid
white
whitegrid
I would like to change the default colormap for pyplots from 'viridis' to 'Dark2'.
I tried:
changing the 'image.cmap' line in the matplotlibrc file
mpl.rcParams['image.cmap'] = 'Dark2'
mpl.pyplot.set_cmap('Dark2')
pyplot.set_cmap('Dark2')
Somehow none of these attempts worked. I also tried restarting the kernel afterwards and also restartet spyder itself but nothing changed. Now Im out of ideas.
import matplotlib as mpl
from matplotlib import pyplot
mpl.rcParams['image.cmap'] = 'Dark2'
mpl.pyplot.set_cmap('Dark2')
pyplot.set_cmap('Dark2')
I am always ending up with the default colors of the viridis colormap which starts with a blueish color and 2nd on orange one. I would like to see the green color from Dark2 first and than the orange one.
Appreciate your help !
cheers, Gerrit
I don't think plt.set_cmap works for your use case. Here are two options that should.
Use Seaborn's helper:
import seaborn as sns
sns.set_palette('Dark2')
Use Maplotlib rcParams:
from cycler import cycler
from matplotlib import pyplot as plt
plt.rcParams['axes.prop_cycle'] = cycler('color', plt.get_cmap('Dark2').colors)
You can use matplotlib.pyplot.set_cmap is the way to change the default colormap. If you run the code below, you should see the 'Dark2' colormap.
import matplotlib.pyplot as plt
import numpy as np
plt.set_cmap('Dark2')
plt.imshow(np.random.random((20, 20)))
plt.colorbar()
plt.show()
I am trying to set the default colormap (not just the color of a specific plot) for matplotlib in my jupyter notebook (Python 3). I found the commands: plt.set_cmap("gray") and mpl.rc('image', cmap='gray'), that should set the default colormap to gray, but both commands are just ignored during execution and I still get the old colormap.
I tried these two codes:
import matplotlib as mpl
mpl.rc('image', cmap='gray')
plt.hist([[1,2,3],[4,5,6]])
import matplotlib.pyplot as plt
plt.set_cmap("gray")
plt.hist([[1,2,3],[4,5,6]])
They should both generate a plot with gray tones. However, the histogram has colors, which correspond to the first two colors of the default colormap. What am I not getting?
Thanks to the comment of Chris, I found the issue, it's not the default colormap that I need to change but the default color cycle. it's described here: How to set the default color cycle for all subplots with matplotlib?
import matplotlib as mpl
import matplotlib.pyplot as plt
from cycler import cycler
# Set the default color cycle
colors=plt.cm.gray(np.linspace(0,1,3))
mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=colors)
plt.hist([[1,2,3],[4,5,6]])
Since you have two data sets your are passing, you'll need to specify two colors.
plt.hist([[1,2,3],[4,5,6]], color=['black','purple'])
You can make use of the color argument in matplotlib plot function.
import matplotlib.pyplot as plt
plt.hist([[1,2,3],[4,5,6]], color=['gray','gray'])
with this method you have to specify the color scheme for each dataset hence an array of colors as I have put it above.
If you are using a version of matplotlib between prio and 2.0 you need to use rcParams (still working in newer versions):
import matplotlib.pyplot as plt
plt.rcParams['image.cmap'] = 'gray'
I'm using the following in a Jupyter notebook, using the latest Anaconda update (including Matplotlib 3.1.1,)
Thanks to SpghttCd, I have the code to do a stacked horizontal bar, but Seaborn puts it on a new plot below the default one.
How might I best fix this problem?
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data=pd.DataFrame(data={"R1":["Yes","Yes","Yes","No","No"]})
freq = data["R1"].value_counts(normalize=True)*100
fig,ax = plt.subplots()
freq.to_frame().T.plot.barh(stacked=True)
You see two axes in Jupyter because you create a fresh one with plt.subplots() and pandas also creates another one.
If you need to reuse an existing axe, pass it to plotting method using ax switch:
fig, axe = plt.subplots()
freq.to_frame().T.plot.barh(stacked=True, ax=axe)
See pandas documentation for details, plotting method always exhibits an ax switch:
ax : Matplotlib axis object, optional
If you accept pandas creates it for you, as #Bharath M suggested, just issue:
axe = freq.to_frame().T.plot.barh(stacked=True)
Then you will see an unique axes and you can access it trough the variable axe.
This question already has answers here:
How can I use seaborn without changing the matplotlib defaults?
(2 answers)
Closed 3 years ago.
Seaborn provides of a handful of graphics which are very interesting for scientifical data representation.
Thus I started using these Seaborn graphics interspersed with other customized matplotlib plots.
The problem is that once I do:
import seaborn as sb
This import seems to set the graphic parameters for seaborn globally and then all matplotlib graphics below the import get the seaborn parameters (they get a grey background, linewithd changes, etc, etc).
In SO there is an answer explaining how to produce seaborn plots with matplotlib configuration, but what I want is to keep the matplotlib configuration parameters unaltered when using both libraries together and at the same time be able to produce, when needed, original seaborn plots.
If you never want to use the seaborn style, but do want some of the seaborn functions, you can import seaborn using this following line (documentation):
import seaborn.apionly as sns
If you want to produce some plots with the seaborn style and some without, in the same script, you can turn the seaborn style off using the seaborn.reset_orig function.
It seems that doing the apionly import essentially sets reset_orig automatically on import, so its up to you which is most useful in your use case.
Here's an example of switching between matplotlib defaults and seaborn:
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
# a simple plot function we can reuse (taken from the seaborn tutorial)
def sinplot(flip=1):
x = np.linspace(0, 14, 100)
for i in range(1, 7):
plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)
sinplot()
# this will have the matplotlib defaults
plt.savefig('seaborn-off.png')
plt.clf()
# now import seaborn
import seaborn as sns
sinplot()
# this will have the seaborn style
plt.savefig('seaborn-on.png')
plt.clf()
# reset rc params to defaults
sns.reset_orig()
sinplot()
# this should look the same as the first plot (seaborn-off.png)
plt.savefig('seaborn-offagain.png')
which produces the following three plots:
seaborn-off.png:
seaborn-on.png:
seaborn-offagain.png:
As of seaborn version 0.8 (July 2017) the graph style is not altered anymore on import:
The default [seaborn] style is no longer applied when seaborn is imported. It is now necessary to explicitly call set() or one or more of set_style(), set_context(), and set_palette(). Correspondingly, the seaborn.apionly module has been deprecated.
You can choose the style of any plot with plt.style.use().
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('seaborn') # switch to seaborn style
# plot code
# ...
plt.style.use('default') # switches back to matplotlib style
# plot code
# ...
# to see all available styles
print(plt.style.available)
Read more about plt.style().
You may use the matplotlib.style.context functionality as described in the style guide.
#%matplotlib inline #if used in jupyter notebook
import matplotlib.pyplot as plt
import seaborn as sns
# 1st plot
with plt.style.context("seaborn-dark"):
fig, ax = plt.subplots()
ax.plot([1,2,3], label="First plot (seaborn-dark)")
# 2nd plot
with plt.style.context("default"):
fig, ax = plt.subplots()
ax.plot([3,2,1], label="Second plot (matplotlib default)")
# 3rd plot
with plt.style.context("seaborn-darkgrid"):
fig, ax = plt.subplots()
ax.plot([2,3,1], label="Third plot (seaborn-darkgrid)")
Restore all RC params to original settings (respects custom rc) is allowed by seaborn.reset_orig() function
As explained in this other question you can import seaborn with:
import seaborn.apionly as sns
And the matplotlib styles will not be modified.