seaborn boxplot: Change color and shape of mean - python-3.x

Simple question that I cannot seem to find the answer to.
How do I change the color and shape of the mean indicator in a Seaborn Boxplot?
It defaults to a Green Triangle and it generally difficult to see.
I've tried to find the answer in both the seaborn documentation, as well as the matplotlib documentation. There is also a related question on stackoverflow where someone asked how to change around colors related to the seaborn boxplots and was able to change everything except for the mean indicator.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
data = [[np.random.rand(100)] for i in range(3)]
sns.boxplot(data=data, showmeans=True)
plt.show()

The keyword argument you are looking for is meanprops. It is in the matplotlib boxplot documentation under "other parameters":
import seaborn as sns
data = [[np.random.rand(100)] for i in range(3)]
sns.boxplot(data=data, showmeans=True,
meanprops={"marker":"s","markerfacecolor":"white", "markeredgecolor":"blue"})
plt.show()

Related

Where/When the interpreter generates the graphic output, seaborn vs matplotlib

My question is about using seaborn and matplotlib together, common practice in many works.
I don't understand what command actually generates the graphic output...
How does the Python interpreter know when to plot the graph?
I used to think sns was drawing the graph, since it would be the last command before a graphic output:
plt.title("Monthly Visitors to Avila Adobe")
plt.xlabel("Date")
sns.lineplot(data=museum_data['Avila Adobe'],label='Avila Adobe')
But I found others examples with inverted calls, plt in last, and the graphic output displayed only after the plt call:
sns.lineplot(data=museum_data['Avila Adobe'],label='Avila Adobe')
plt.title("Monthly Visitors to Avila Adobe")
plt.xlabel("Date")
The two codes above do exact the same graphics.
I understand seaborn is build on top of matplotlib.
But,
I don't understand where/when the code generates the graphic output: after the sns or after the plt?
What statement draw the graph?
If my rationalization is wrong, pls clarify why.
Seaborn and Matplotlib are two of Python's most powerful visualization libraries. Seaborn uses fewer syntax and has stunning default themes and Matplotlib is more easily customizable through accessing the classes.
Seaborn
The seaborn package was developed based on the Matplotlib library. It is used to create more attractive and informative statistical graphics. While seaborn is a different package, it can also be used to develop the attractiveness of matplotlib graphics.
To answer your question, When we load seaborn into the session, everytime a matplotlib plot is executed, seaborn's default customizations are added. However, a huge problem that troubles many users is that the titles can overlap. Combine this with matplotlib's only confusing naming convention for its titles it becomes a nuisance. Nevertheless, the attractive visuals still make it usable for every Data Scientist's work.
In order to get the titles in the fashion that we want and have more customizability, We need to use the seaborn & matplotlib structure . Note that this is only necessary if we use subtitles in our plots. Sometimes they are necessary so it is better to have it on hand. Refer below code for more details.
matplotlib style plot :
import matplotlib.pyplot as plt
import numpy as np
# using some dummy data for this example
xs = np.random.randint( 0, 10, size=10)
ys = np.random.randint(-5, 5, size=10)
# plot the points
fig = plt.figure(figsize=(12,6))
fig.suptitle('Matplotlib with Python', fontsize='x-large', fontweight='bold')
plt.subplot(121)
plt.scatter(xs,ys,c='b') # scatter graph plotted from this line
plt.grid()
plt.subplot(122)
plt.plot(xs,ys,'bo--') # line graph plotted from this line
plt.grid()
Seaborn style plot :
import seaborn as sns
sns.set()
fig = plt.figure()
fig.suptitle('Seaborn with Python', fontsize='x-large', fontweight='bold')
fig.subplots_adjust(top=0.87)
#This is used for the main title. 'figure()' is a class that provides all the plotting elements of a diagram.
#This must be used first or else the title will not show.fig.subplots_adjust(top=0.85) solves our overlapping title problem.
fig = plt.figure(figsize=(12,6))
ax = fig.add_subplot(121)
fontdict={'fontsize': 14,
'fontweight' : 'book',
'verticalalignment': 'baseline',
'horizontalalignment': 'center'}
ax.set_title('Scatter Plot Tutorial', fontdict=fontdict)
#This specifies which plot to add the customizations. fig.add_sublpot(121) corresponds to top left plot no.1
plt.plot(xs, ys, 'bo' ) # scatter graph plotted from this line in seaborn with matplotlib command & seaborn style
plt.xlabel('x-axis', fontsize=14)
plt.ylabel('yaxis', fontsize=14)
ax = fig.add_subplot(122)
fontdict={'fontsize': 14,
'fontweight' : 'book',
'verticalalignment': 'baseline',
'horizontalalignment': 'center'}
ax.set_title('Line Plot Tutorial', fontdict=fontdict)
sns.lineplot(xs, ys) # line graph plotted from this line in seaborn with seaborn command & seaborn style
plt.xlabel('x-axis', fontsize=14)
plt.ylabel('yaxis', fontsize=14);
Now compare both plots stylewise and please read code comments carefully.
source
Python seaborn tutorial
I posted this question 6 months ago, so, now I'm 6 months more experienced!
How does the Python interpreter know when to plot the graph?
I don't understand where/when the code generates the graphic output: after the sns or after the plt?
What I learn:
Well, I was used to a approach where after a given command and I get a response.
But that is no the case when working with graphics in Python, specially when using a notebook...
Actually, using matplotlib, sns, etc, we can, in a cumulative way, prepare many aspects before actually output the graphic.
And many commands (functions, methods) are capable to do the real display, but it is the last "complete" command (my interpretation here) does the job.
So, since they are consistent, the order is not a problem.
Sure, we need to assure the good logic!
It was that I would like to understand.
The #jay-patel answer helped me to think about it, he give me good examples.

Matplotlib & LaTeX

I use MikTeX and try to obtain LaTeX fonts in my matplotlib plots.
However, using the demo code, Jyputer Notebook says that there is no latex,
Failed to process string with tex because latex could not be found
I try to add into PATH the path to latex.exe, dvipng.exe and ghostscript. Unfortunately, it still does not work. What I do wrong?
If I evaluate the following
import matplotlib.pyplot as plt
import numpy as np
plt.plot(np.sin(np.arange(0, 10, 0.1)),label=r"$\mathcal{M}=2$")
plt.xlabel("x")
plt.ylabel("y")
plt.legend()
it returns me the next picture,
So, I see that \mathcal{} command works perfectly, whereas the fonts are not "latex".
You need to add plt.rc('text', usetex=True).

Plotting a 3D scatter plot using Python only returns an empty space (Jupyter Notebook)

I am trying to create a 3D scatter plot using matplotlib in a Jupyter Notebook page. The code is not returning any errors, but I have yet to have the plot actually show up. The output is just blank.
Python: 3.7.3
Matplotlib: 3.0.3
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
%matplotlib inline
%matplotlib notebook
threedee = plt.figure().gca(projection='3d')
threedee.scatter(existing_df_2d.PC1, existing_df_2d.PC2,
existing_df_2d.data_mean)
plt.show()
I included an example of the output (it's blank):
You are using two backends
%matplotlib inline
%matplotlib notebook
As a result, there seems to be a conflict between the two backends when invoked in parallel one after the other.
P.S: When I tried putting %matplotlib notebook in the same cell as the rest of the code, I did not see any figure. When I put it in a different cell, I see the figure.
Solution: Just use either the %matplotlib inline or %matplotlib notebook in a new separate cell and things will work fine
In my experience, %matplotlib notebook doesn't work with 3D plots unfortunately. Just use %matplotlib inline and you should be OK.

Seaborn style not being applied

I just updated conda and installed seaborn (v0.9.0). Then started jupyter notebook and tried to plot a seaborn distplot, but strangely it has matplotlib style!
Could someone please help me apply seaborn style with seaborn plots.
import seaborn as sns
%matplotlib inline
sns.distplot(tips['total_bill'])
Try using
sns.set()
after %matplotlib inline as following and see if it works then.
import seaborn as sns
%matplotlib inline
sns.set()
sns.distplot(tips['total_bill'])

Controlling the background in Python's matplotlib

I want to have blank background in my figure, however, it seems that the for some reason the default is not. Here is an example:
import matplotlib.pyplot as plt
x=[1,2]
y=[3,4]
plt.plot(x,y)
This gives me the following figure:
Why do I get this gridded grey background by default? How would one change the default? And perhaps also how would that differ from setting it only for one figure without changing defaults? Thanks
Edit: Apparently, this happened because I imported the seaborn module, as the answer suggested. But why does this behavior occur? So if I want to use both seaborn and matplotlib in one script, I need to keep setting the default background?
What you show in the question isn't actually the matplotlib default style. You may get this because you may have imported some other modules.
To get back the default style use
plt.rcParams.update(plt.rcParamsDefault)
When e.g. seaborn is imported it sets its own style. This is a very greedy behaviour, but you can of course set another style afterwards
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('seaborn-paper')
You may want to look at the style reference.
This question may be of interest when no other style is desired. The idea is, to only load the API, without the styles from seaborn
import seaborn.apionly as sns

Resources