Show mean in the box plot in python? - python

I am new to Matplotlib, and as I am learning how to draw box plot in python, I was wondering if there is a way to show mean in the box plots?
Below is my code..
from pylab import *
import matplotlib.pyplot as plt
data1=np.random.rand(100,1)
data2=np.random.rand(100,1)
data_to_plot=[data1,data2]
#Create a figure instance
fig = plt.figure(1, figsize=(9, 6))
# Create an axes instance
axes = fig.add_subplot(111)
# Create the boxplot
bp = axes.boxplot(data_to_plot,**showmeans=True**)
Even though I have showmean flag on, it gives me the following error.
TypeError: boxplot() got an unexpected keyword argument 'showmeans'

This is a minimal example and produces the desired result:
import matplotlib.pyplot as plt
import numpy as np
data_to_plot = np.random.rand(100,5)
fig = plt.figure(1, figsize=(9, 6))
ax = fig.add_subplot(111)
bp = ax.boxplot(data_to_plot, showmeans=True)
plt.show()
EDIT:
If you want to achieve the same with matplotlib version 1.3.1 you'll have to plot the means manually. This is an example of how to do it:
import matplotlib.pyplot as plt
import numpy as np
data_to_plot = np.random.rand(100,5)
positions = np.arange(5) + 1
fig, ax = plt.subplots(1,2, figsize=(9,4))
# matplotlib > 1.4
bp = ax[0].boxplot(data_to_plot, positions=positions, showmeans=True)
ax[0].set_title("Using showmeans")
#matpltolib < 1.4
bp = ax[1].boxplot(data_to_plot, positions=positions)
means = [np.mean(data) for data in data_to_plot.T]
ax[1].plot(positions, means, 'rs')
ax[1].set_title("Plotting means manually")
plt.show()
Result:

You could also upgrade the matplotlib:
pip2 install matplotlib --upgrade
and then
bp = axes.boxplot(data_to_plot,showmeans=True)

Related

Manually Managing Axes on python [duplicate]

This question already has answers here:
Matplotlib different size subplots
(6 answers)
Closed 1 year ago.
I was trying to achieve this
But i ended with this
The main idea was to manage axes to include the third subplot in the figure, but i can't find a way to do it. Can somebody help with that please.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-4*np.pi,4*np.pi,0.25)
np.sincx=np.sin(x)/x
plt.figure(num=3, figsize=(7,5))
plt.subplot(3,2,1)
plt.plot(x,np.sincx)
plt.subplot(3,2,2)
plt.plot(x,np.sincx,"ro")
fig = plt.figure()
ax = fig.add_axes((0.125,0.1,0.775,0.45))
plt.plot(x,np.sincx**2)
In your code, you are creating two figure, one with the two tops plots, and one with the bottom one. You need to only create one with multiple subplots!
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-4*np.pi,4*np.pi,0.25)
np.sincx = np.sin(x)/x
ax1 = plt.subplot(221)
ax1.plot(x,np.sincx)
ax2 = plt.subplot(222)
ax2.plot(x,-np.sincx,"ro")
ax3 = plt.subplot(212)
ax3.plot(x, np.sincx**2)
plt.show()
Output
With add_axes()
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-4*np.pi,4*np.pi,0.25)
np.sincx = np.sin(x)/x
fig = plt.figure()
ax1 = plt.subplot(221)
ax1.plot(x,np.sincx)
ax2 = plt.subplot(222)
ax2.plot(x,-np.sincx,"ro")
ax3 = fig.add_axes((0.125,0.1,0.775,0.45))
ax3.plot(x, np.sincx**2)
plt.show()
Output add_axes()

Bothering frame on matplotlib 3D plots

I make 3d plots with matplotlib and I always get a weird frame with a normalized scale around my plot. Where does it come from and how can I get rid of it ?
Here is an example code that drives me to the problem :
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
x = np.linspace(0,10)
y = np.linspace(0,10)
z = np.linspace(0,10)
# ------------- Figure ---------------
fig, ax = plt.subplots(figsize = (9,6))
ax = fig.gca(projection='3d')
ax.plot(np.sin(x), np.cos(y), z)
plt.show()
And here is the result :
I use plt.subplots() because I want a figure with a 3D and a 2D plot side by side.
You call plt.subplots(...) and this, of course, instantiates an Axes, complete of horizontal and vertical spines, before Matplotlib is informed that you want a 3D enabled Axes.
When you later call plt.gca(...) it's too late…
Simply use
fig, ax = plt.subplots(figsize = (9,6), subplot_kw={"projection" : "3d"})
or
fig = plt.figure(figsize = (9,6))
ax = fig.add_subplot(111, projection='3d')
Addressing OP's comment
Figure.add_subplot is pretty flexible…
fig = plt.figure()
fig.add_subplot(1,5,(1,4), projection='3d')
fig.add_subplot(1,5,5)
fig.tight_layout()
plt.show()

Automatically updated graph in jupyter notebook using qt5

how to do this in jupyter notebook:
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
m = 100
n = 100
matrix = np.random.normal(0,1,m*n).reshape(m,n)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.ion()
fig.show()
fig.canvas.draw()
for i in range(0,100):
#ax.clear()
plt.plot(matrix[i,:])
fig.canvas.draw()
but using "%matplotlib qt5" instead of "notebook"?
When I try it it show the figure only after the loop is ended. I would like to see it updating every plot.
In principle you can do the following in interactive mode:
%matplotlib qt4
import numpy as np
import matplotlib.pyplot as plt
m = 100
n = 100
matrix = np.random.normal(0,1,m*n).reshape(m,n)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.ion()
plt.draw()
for i in range(0,30):
#ax.clear()
plt.plot(matrix[i,:])
plt.draw()
plt.pause(0.1)
plt.ioff()
plt.show()
However, it might crash in jupyter after finishing the loop, due to Qt being used.
It should however work, when using the tk backend, %matplotlib tk.
You might want to consider using a FuncAnimation instead of interactive mode. While this would suffer from the same limitations with Qt in Jupyter I find it more intuitive to use a function for updating the plot and there is no need to redraw the canvas manually.
%matplotlib tk
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation
m = 100
n = 100
matrix = np.random.normal(0,1,m*n).reshape(m,n)
fig = plt.figure()
ax = fig.add_subplot(111)
def update(i):
#ax.clear()
plt.plot(matrix[i,:])
ani = matplotlib.animation.FuncAnimation(fig, update, frames=30, repeat=False)
plt.show()
I asked a new question on why this would no work with the qt backend.

Plots that varies over the time on Python Matplotlib with Jupyter

In the following lines I report a code that generates a plot changing over the time with Python on Anaconda Spyder
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-3, 3, 0.01)
N = 1
fig = plt.figure()
ax = fig.add_subplot(111)
for N in range(8):
y = np.sin(np.pi*x*N)
line, = ax.plot(x, y)
plt.draw()
plt.pause(0.5)
line.remove()
I would like to do the some with Jupyter, but it is not possible. Particularly it seems that the Matplotlib method .pause() does not exist on Jupyter.
Is there anyone who can explain me this difference and can help me building up a code for plots variating over the time with Python on Jupyter, please?
It works for me if I select an interactive backend using the magic command %matplotlib; it is likely that your Jupyter notebook settings are set to display plots inline.
import matplotlib.pyplot as plt
import numpy as np
%matplotlib
x = np.arange(-3, 3, 0.01)
N = 1
fig = plt.figure()
ax = fig.add_subplot(111)
for N in range(8):
y = np.sin(np.pi*x*N)
line, = ax.plot(x, y)
plt.draw()
plt.pause(0.5)
line.remove()
To restore your setings, use the magic %matplotlib inline

Set scale of axis in plot using matplotlib

I am unable to scale the y-axis. My code is as follows:
import matplotlib.pyplot as pt
import numpy as np
fig = pt.figure()
ax = fig.add_subplot(111)
sample = 20
x=np.arange(sample)
y=10*np.sin(2*np.pi*x/20)
pt.plot(x,y)
pt.show()
The y axis has scale of 5. I'm trying to make it 1.
You can do so using set_yticks this way:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
sample = 20
x=np.arange(sample)
y=10*np.sin(2*np.pi*x/20)
ax.plot(x,y)
ax.set_yticks(np.arange(min(y), max(y)+1, 1.0)) # setting the ticks
ax.set_xlabel('x')
ax.set_ylabel('y')
fig.show()
Which produces this image wherein y-axis has a scale of 1.

Categories

Resources