Python Matplotlib/Seaborn/Jupyter - Putting bar plot in wrong place? - python

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.

Related

How to get visible bars on a histogram/distribution plot? [duplicate]

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

Converting a single dynamic plot into dynamic plot with subplots in jupyter notebook

I have the code below, which works great with a single plot, but I'm trying to create a new plot with 1x2 subplots. The second plot will be identical to the first, just in another subplot.
# This code works fine as a single plot
%matplotlib inline
import time
import pylab as pl
from IPython import display
for i in range(10):
pl.clf()
pl.plot(pl.randn(100))
display.display(pl.gcf())
display.clear_output(wait=True)
time.sleep(1.0)
I'm not familar with pylab, but the above plot runs so smoothly compared to the pyplot code I found on the nex, that I'm trying to figure out how to implement this code with subplots.
#can't implement it to a plot with subplots
%matplotlib inline
import time
import pylab as pl
from IPython import display
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, sharex = True, figsize = (10,5))
for i in range(10):
pl.clf()
ax1.plot(pl.randn(100),)
ax2.plot(pl.randn(50))
display.display(pl.show())
display.clear_output(wait=True)
time.sleep(1.0)
However, no graph is being outputted with my attempt.
I'm played around with this code, but I can't seem to make it work cleanly.
thank you.
To visualize the plot with subplots, you should know the differences between Figure and Axes in matplotlib. Basically, axes belong to the figure, and you want to plot your data in the axes, but display the figure. Both Figure and Axes instances can be obtained with a single call to pl.subplots(nrow, ncol). See if the code below does what you want:
%matplotlib inline
import time
import pylab as pl
from IPython import display
for i in range(10):
pl.clf()
f, ax = pl.subplots(1, 2)
ax[0].plot(pl.randn(100))
ax[1].plot(pl.randn(100))
display.display(f)
display.clear_output(wait=True)
time.sleep(1.0)

No outlines on bins of Matplotlib histograms or Seaborn distplots

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

Plot semilogx with matplotlib then convert it into Bokeh

I plot a figure containing several curves using matplotlib and then try to convert it into bokeh:
import numpy as np
import matplotlib.pyplot as plt
from bokeh import mpl
from bokeh.plotting import show, output_file
num_plots = 6
colormap = plt.cm.gist_ncar
time = np.random.random_sample((300, 6))
s_strain = np.random.random_sample((300, 6))
def time_s_strain_bokeh(num_plots, colormap, time, s_strain):
plt.gca().set_color_cycle([colormap(i) for i in np.linspace(0, 0.9, num_plots)])
plt.figure(2)
for i in range(0, num_plots):
plt.plot(time[:,i], s_strain[:,i])
plt.grid(True)
# save it to bokeh
output_file('anywhere.html')
show(mpl.to_bokeh())
time_s_strain_bokeh(num_plots, colormap, time, s_strain)
it works fine. However, I want to have a semilogx plot. When I change plt.plot in the "for" loop into plt.semilogx, I have the following error:
UnboundLocalError: local variable 'laxis' referenced before assignment
What can I do to change the x-axis onto log scale?
I'm with the same issue! 1/2 of the solution is this (supose my data is in a Pandas dataframe called pd):
pd.plot(x='my_x_variable', y='my_y_variable)
p = mpl.to_bokeh()
p.x_mapper_type='log' # I found this property with p.properties_with_values()
show(p)
I edited this answare because I just found part 2/2 of the solution:
When I use just the code above, the plot is semilog (ok!), but the x axis is flipped (mirrored)!!!
The solution I found is explicitly redefine xlim:
p.x_range.start=0.007 # supose pd['my_x_variable'] starts at 0.007
p.x_range.end=0.17 # supose pd['my_x_variable'] ends at 0.17
With this my plot became identical with the matplotlib original plot. The final code looks like:
pd.plot(x='my_x_variable', y='my_y_variable)
p = mpl.to_bokeh()
p.x_mapper_type='log'
p.x_range.start= pd['my_x_variable'].iloc[1] # numpy start at 0, take care!
p.x_range.end= pd['my_x_variable'].iloc[-1]
show(p)
As of Bokeh 0.12, partial and incomplete MPL compatibility is provided by the third party mplexporter library, which now appears to be unmaintained. Full (or at least, much more complete) MPL compat support will not happen until the MPL team implements MEP 25. However, implementing MEP 25 is an MPL project task, and the timeline/schedule is entirely outside of the control of the Bokeh project.
The existing MPL compat based on mplexporter is provided "as-is" in case it is useful in the subset of simple situations that it currently works for. My suggestion is to use native Bokeh APIs directly for anything of even moderate complexity.
You can find an example of a semilog plot created using Bokeh APIs here:
http://docs.bokeh.org/en/latest/docs/user_guide/plotting.html#log-scale-axes

Seaborn configuration hides default matplotlib [duplicate]

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.

Categories