Matplotlib Table's Font Size - python

Working with Matplotlib in Python (2.7.9). I have to plot a table in a subplot (in this case subplot name is tab) but I can't seem to find a way to change the font size of the table (http://imgur.com/0Ttvzee - bottom left). Antman is happy about the results, I am not.
This is the code I've been using.
EDIT: Added full code
def stat_chart(self):
DN = self.diff
ij = self.ij_list
mcont = self.mcont
ocont = self.ocont
ucont = self.ucont
dist = self.widths
clon = '%1.2f' %self.mclon
clat = '%1.2f' %self.mclat
clonlat = "{0}/{1}".format(clon,clat)
area = self.area
perim = self.perimeter
mdist = np.array(self.widths)
mdist = mdist[:,0]*10
mdist = np.mean(mdist)
pstat = self.polygon_status
if pstat == 1:
status = "Overestimation"
else:
status = "Underestimation"
# Setting up the plot (2x2) and subplots
fig = plt.figure()
gs = gridspec.GridSpec(2,2,width_ratios=[2,1],height_ratios=[4,1])
main = plt.subplot(gs[0,0])
polyf = plt.subplot(gs[0,1])
tab = plt.subplot(gs[1,0])
leg = plt.subplot(gs[1,1])
tab.set_xticks([])
leg.set_xticks([])
tab.set_yticks([])
leg.set_yticks([])
tab.set_frame_on(False)
leg.set_frame_on(False)
# Main image on the top left
main.imshow(DN[::-1],cmap='winter')
x1,x2,y1,y2 = np.min(ij[:,1])-15,np.max(ij[:,1])+15,np.min(ij[:,0])-15,np.max(ij[:,0])+15
main.axvspan(x1,x2,ymin=1-((y1-320)/float(len(DN)-320)),ymax=1-((y2-320)/float(len(DN)-320)),color='red',alpha=0.3)
main.axis([0,760,0,800])
# Polygon image on the top right
polyf.imshow(DN,cmap='winter')
polyf.axis([x1,x2,y2,y1])
polyf.plot(mcont[:,1],mcont[:,0],'ro',markersize=4)
polyf.plot(ocont[:,1],ocont[:,0],'yo',markersize=4)
polyf.plot(ucont[:,1],ucont[:,0],'go',markersize=4)
for n,en in enumerate(dist):
polyf.plot([en[2],en[4]],[en[1],en[3]],color='grey',alpha=0.3)
# Legend on the bottom right
mc = mlines.Line2D([],[],color='red',marker='o')
oc = mlines.Line2D([],[],color='yellow',marker='o')
uc = mlines.Line2D([],[],color='green',marker='o')
ed = mlines.Line2D([],[],color='black',alpha=0.5)
pos_p = mpatches.Patch(color='lightgreen')
neg_p = mpatches.Patch(color='royalblue')
leg.legend([mc,oc,uc,ed,pos_p,neg_p],("Model Cont.","Osisaf Cont.","Unknown Cont.","Dist. Mdl to Osi", \
'Model Overestimate','Model Underestimate'),loc='center')
# Statistics table on the bottom left
stats = [[clonlat+' degrees' ,'%1.4E km^2' %area,'%1.4E km' %perim,'%1.4f km' %mdist,status]]
columns = ('Center Lon/Lat','Area','Perimeter','Mean Width','Status')
rows = ['TODOpolyname']
cwid = [0.1,0.1,0.1,0.1,0.1,0.1]
the_table = tab.table(cellText=stats,colWidths=cwid,rowLabels=rows,colLabels=columns,loc='center')
table_props = the_table.properties()
table_cells = table_props['child_artists']
for cell in table_cells: cell.set_height(0.5)
plt.show()
return
EDIT2: Eventually (un)solved plotting text instead of table. Good enough.

I had a similar issue in changing the fontsize. Try the following
the_table.auto_set_font_size(False)
the_table.set_fontsize(5.5)
Worked for me.

According to the docs, table has a kwarg called fontsize, a float value for the size in points.
In your example from above, for a fontsize of 5 points you would use:
the_table =tab.table(cellText=stats,colWidths=cwid,rowLabels=rows,colLabels=columns,loc='center',fontsize=5)
If you require greater control, you can pass a FontManager instance to the cell.set_text_props() method as described in this example. That would enable you to set the family, spacing, style etc, in addition to the size.
EDIT: Playing around with Matplotlib's example, it seems that just passing fontsize to the table has no effect. However, importing
from matplotlib.font_manager import FontProperties
and then looping through the cells and running
cell.set_text_props(fontproperties=FontProperties(size = 5))
does have the desired effect. It is unclear why the documented kwarg fontsize does not work in this (or apparently in your) case.

Related

How to set widgets to link to array for jupyternotebooks

I am trying to set an interactive notebook up that plots some interpolated GPS data. I have the plotting working by itself, but I am trying to use the ipython widgets to make it more interactive for others.
Currently, my plotting looks like this
def create_grid(array,spacing=.01):
'''
creates evenly spaced grid from the min and max of an array
'''
grid = np.arange(np.amin(array), np.amax(array),spacing)
return grid
def interpolate(x, y, z, grid_spacing = .01, model='spherical',returngrid = False):
'''Interpolates z value and uses create_grid to create a grid of values based on min and max of x and y'''
grid_x = create_grid(x,spacing = grid_spacing)
grid_y = create_grid(y, spacing = grid_spacing)
OK = OrdinaryKriging(x, y, z, variogram_model=model, verbose = False,\
enable_plotting=False, nlags = 20)
z1, ss1 = OK.execute('grid', grid_x,grid_y,mask = False)
print('Interpolation Complete')
vals=np.ma.getdata(z1)
sigma = np.ma.getdata(ss1)
if returngrid == False:
return vals,sigma
else:
return vals, sigma, grid_x, grid_y
mesh_x, mesh_y = np.meshgrid(grid_x,grid_y)
plot = plt.scatter(mesh_x, mesh_y, c = z1, cmap = cm.hsv)
cb = plt.colorbar(plot)
cb.set_label('Northing Change')
plt.show()
'''
This works currently, but I am trying to set up a widget to change the variogram model in the kriging interpolation, as well as change the field to be interpolated.
Currently, to do that I have:
def update_plot(zfield,variogram):
plt.clf()
z1, ss1, grid_x,grid_y =interpolate(lon,lat,zfield,returngrid= True,model=variogram)
mesh_x, mesh_y = np.meshgrid(grid_x,grid_y)
plot = plt.scatter(mesh_x, mesh_y, c = z1, cmap = cm.hsv)
cb = plot.colorbar(plot)
cb.set_label('Interpolated Value')
variogram = widgets.Dropdown(options = ['linear', 'power', 'gaussian', 'spherical', 'exponential', 'hole-effect'],
value = 'spherical', description = "Variogram model for interpolation")
zfield = widgets.Dropdown(options = {'Delta N':delta_n, 'Delta E': delta_e,'Delta V':delta_v},value = 'Delta N',
description = 'Interpolated value')
widgets.interactive(update_plot, variogram = variogram,zfield =zfield)
Which brings up the error
TraitError: Invalid selection: value not found
the values delta_n, delta_e and delta_v are numpy arrays. I have tried looking at documentation but it is not as detailed as something like matplotlibs documentation or something so I feel like I am kind of flying blind here.
Thank you
In this line, you specify the possible values of the Dropdown as:
zfield = widgets.Dropdown(options = {'Delta N':delta_n, 'Delta E': delta_e,'Delta V':delta_v}
When a mapping is used, the values of the dict are interpreted as the possible options. So value = 'Delta N' causes an error as this is not one of the possible values of the Dropdown (although it is one of the keys in the mapping dict). I believe you want value = delta_n instead.

Python plotting to different figures fails

EDIT: I figured out that the Problem always occours if one tries to plot to two different lists of figures. Does that mean that one can not do plots to different figure-lists in the same loop? See latest code for much simpler sample of a problem.
I try to analyze a complex set of data which consists basically about measurements of electric devices under different conditions. Hence, the code is a bit more complex but I tried to strip it down to a working example - however it is still pretty long. Hence, let me explain what you see: You see 3 classes with Transistor representing an electronic device. It's attribute Y represents the measurement data - consisting of 2 sets of measurements. Each Transistor belongs to a group - 2 in this example. And some groups belong to the same series - one series where both groups are included in this example.
The aim is now to plot all measurement data for each Transistor (not shown), then to also plot all data belonging to the same group in one plot each and all data of the same series to one plot. In order to program it in an efficent way without having a lot of loops my idea was to use the object orientated nature of matplotlib - I will have figures and subplots for each level of plotting (initialized in initGrpPlt and initSeriesPlt) which are then filled with only one loop over all Transistors (in MainPlt: toGPlt and toSPlt). In the end it should only be printed / saved to a file / whatever (PltGrp and PltSeries).
The Problem: Even though I specify where to plot, python plots the series plots into the group plots. You can check this yourself by running the code with the line 'toSPlt(trans,j)' and without. I have no clue why python does this because in the function toSPlt I explicetly say that python should use the subplots from the series-subplot-list. Would anyone have an idea to why this is like this and how to solve this problem in an elegent way?
Read the code from the bottom to the top, that should help with understanding.
Kind regards
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
maxNrVdrain = 2
X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
A = [[1*np.cos(X),2*np.cos(X),3*np.cos(X),4*np.cos(X)],[1*np.tan(X),2*np.tan(X),3*np.tan(X),4*np.tan(X)]]
B = [[2* np.sin(X),4* np.sin(X),6* np.sin(X),8* np.sin(X)],[2*np.cos(X),4*np.cos(X),6*np.cos(X),8*np.cos(X)]]
class Transistor(object):
_TransRegistry = []
def __init__(self,y1,y2):
self._TransRegistry.append(self)
self.X = X
self.Y = [y1,y2]
self.group = ''
class Groups():
_GroupRegistry = []
def __init__(self,trans):
self._GroupRegistry.append(self)
self.transistors = [trans]
self.figlist = []
self.axlist = []
class Series():
_SeriesRegistry = []
def __init__(self,group):
self._SeriesRegistry.append(self)
self.groups = [group]
self.figlist = []
self.axlist = []
def initGrpPlt():
for group in Groups._GroupRegistry:
for j in range(maxNrVdrain):
group.figlist.append(plt.figure(j))
group.axlist.append(group.figlist[j].add_subplot(111))
return
def initSeriesPlt():
for series in Series._SeriesRegistry:
for j in range(maxNrVdrain):
series.figlist.append(plt.figure(j))
series.axlist.append(series.figlist[j].add_subplot(111))
return
def toGPlt(trans,j):
colour = cm.rainbow(np.linspace(0, 1, 4))
group = trans.group
group.axlist[j].plot(trans.X,trans.Y[j], color=colour[group.transistors.index(trans)], linewidth=1.5, linestyle="-")
return
def toSPlt(trans,j):
colour = cm.rainbow(np.linspace(0, 1, 2))
series = Series._SeriesRegistry[0]
group = trans.group
if group.transistors.index(trans) == 0:
series.axlist[j].plot(trans.X,trans.Y[j],color=colour[series.groups.index(group)], linewidth=1.5, linestyle="-", label = 'T = nan, RH = nan' )
else:
series.axlist[j].plot(trans.X,trans.Y[j],color=colour[series.groups.index(group)], linewidth=1.5, linestyle="-")
return
def PltGrp(group,j):
ax = group.axlist[j]
ax.set_title('Test Grp')
return
def PltSeries(series,j):
ax = series.axlist[j]
ax.legend(loc='upper right', frameon=False)
ax.set_title('Test Series')
return
def MainPlt():
initGrpPlt()
initSeriesPlt()
for trans in Transistor._TransRegistry:
for j in range(maxNrVdrain):
toGPlt(trans,j)
toSPlt(trans,j)#plots to group plot for some reason
for j in range(maxNrVdrain):
for group in Groups._GroupRegistry:
PltGrp(group,j)
plt.show()
return
def Init():
for j in range(4):
trans = Transistor(A[0][j],A[1][j])
if j == 0:
Groups(trans)
else:
Groups._GroupRegistry[0].transistors.append(trans)
trans.group = Groups._GroupRegistry[0]
Series(Groups._GroupRegistry[0])
for j in range(4):
trans = Transistor(B[0][j],B[1][j])
if j == 0:
Groups(trans)
else:
Groups._GroupRegistry[1].transistors.append(trans)
trans.group = Groups._GroupRegistry[1]
Series._SeriesRegistry[0].groups.append(Groups._GroupRegistry[1])
return
def main():
Init()
MainPlt()
return
main()
latest example that does not work:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
Y1 = np.cos(X)
Y2 = np.sin(X)
figlist1 = []
figlist2 = []
axlist1 = []
axlist2 = []
for j in range(4):
figlist1.append(plt.figure(j))
axlist1.append(figlist1[j].add_subplot(111))
figlist2.append(plt.figure(j))#this should be a new set of figures!
axlist2.append(figlist2[j].add_subplot(111))
colour = cm.rainbow(np.linspace(0, 1, 4))
axlist1[j].plot(X,j*Y1, color=colour[j], linewidth=1.5, linestyle="-")
axlist1[j].set_title('Test Grp 1')
colour = cm.rainbow(np.linspace(0, 1, 4))
axlist2[j].plot(X,j*Y2, color=colour[int(j/2)], linewidth=1.5, linestyle="-")
axlist2[j].set_title('Test Grp 2')
plt.show()
Ok, stupid mistake if one thinks of the Background but maybe someone has a similar Problem and is unable to see the cause as I was first. So here is the solution:
The Problem is that the Name of the listobjects like figlist1[j] do not define the figure - they are just pointers to the actual figure object. and if such an object is created by plt.figure(j) one has to make sure that j is different for each figure - hence, in a Loop where multiple figures shall be initialized one Needs to somehow Change the number of the figure or the first object will be overwritten. Hope that helps! Cheers.

Multiple density plot with plotly in R

I really like this python example:https://plot.ly/python/distplot/ scroll to Plot Multiple Datasets. I would expect the exact same thing is available for R, but it's not documented. Does this mean it's not possible? I came across this example https://community.plot.ly/t/r-plotly-overlay-density-histogram/640/4 which I find far less nice.
This doesn't work but would give an idea about the data I use.
# Add histogram data
x1 = data.table(a=rnorm(n = 200,mean = 0,sd = .1), by='Group1')
x2 = data.table(a=rnorm(n = 200,mean = 1,sd = .15), by='Group2')
x3 = data.table(a=rnorm(n = 200,mean = 2,sd = .2), by='Group3')
x4 = data.table(a=rnorm(n = 200,mean = 3,sd = .25), by='Group4')
agg <- rbind(x1,x2,x3,x4)
plot_ly(data = agg, type = "histogram",histnorm, name = "Histogram",group_by='by')
plot_ly(data = agg, type = "density",histnorm, name = "Density",group_by='by')
I'm not entirely sure which critical element you are missing in R, but here is a plotly-based density plus rug plot example based on your sample data.
This is the static ggplot version.
require(ggplot2);
gg <- ggplot(agg, aes(x = a, colour = by)) + geom_density() + geom_rug();
And the interactive ggplotlyed version including screenshot.
require(plotly);
ggplotly(gg);
You can also add a histogram with e.g.
gg + geom_histogram(aes(y = ..density.., fill = by), alpha = 0.2, bins = 50)

Bokeh equivalent of Matplotlib scatter_matrix

Is there a better way of reproducing matplotlibs scatter_matrix (plot all data against all data) in Bokeh than the code below:
defaults.width = 100
defaults.height = 100
scatter_plots = []
y_max = len(dataset.columns)-1
for i, y_col in enumerate(dataset):
for j, x_col in enumerate(dataset):
df = pd.DataFrame({x_col: dataset[x_col].tolist(), y_col: dataset[y_col].tolist()})
p = Scatter(df, x=x_col, y=y_col)
if j > 0:
p.yaxis.axis_label = ""
p.yaxis.visible = False
if i < y_max:
p.xaxis.axis_label = ""
p.xaxis.visible = False
scatter_plots.append(p)
grid = gridplot(scatter_plots, ncols = len(dataset.columns))
show(grid)
In particular I would like to be able to zoom and pan the entire grid of plots as a single entity rather than zoom/pan the subplot the mouse is hovering over.
In general, to have linked panning/zooming, you share the ranges that you want to be linked between plots. This is described here in the Users Guide:
https://docs.bokeh.org/en/latest/docs/user_guide/interaction/linking.html
You can also check out this linked SPLOM example:
https://github.com/bokeh/bokeh/blob/master/examples/models/iris_splom.py
That example is longer/more verbose because it uses the low level bokeh.models API. The important part is where it re-uses the ranges xdr and ydr on ever plot that gets created.
In your particular case, since high level charts don't accept range parameters up front (IIRC), I think you'll have to fix up the charts "after the fact", so maybe something like:
xr = scatter_plots[0].x_range
yr = scatter_plots[0].y_range
for p in scatter_plots:
p.x_range = xr
p.y_range = yr
In case it is useful, I faced the same problem. In actual fact you don't want all the axis linked - but rather each rows y-axis linked and each columns x-axis linked. I'm surprised that this isn't a built in bokeh feature. even iris the example gets this wrong:
http://docs.bokeh.org/en/latest/docs/gallery/iris_splom.html
Here's a code snippet I used:
def scatter_matrix(dataset):
dataset_source = ColumnDataSource(data=dataset)
scatter_plots = []
y_max = len(dataset.columns)-1
for i, y_col in enumerate(dataset.columns):
for j, x_col in enumerate(dataset.columns):
p = figure(plot_width=100, plot_height=100, x_axis_label=x_col, y_axis_label=y_col)
p.circle(source=dataset_source,x=x_col, y=y_col, fill_alpha=0.3, line_alpha=0.3, size=3)
if j > 0:
p.yaxis.axis_label = ""
p.yaxis.visible = False
p.y_range = linked_y_range
else:
linked_y_range = p.y_range
p.plot_width=160
if i < y_max:
p.xaxis.axis_label = ""
p.xaxis.visible = False
else:
p.plot_height=140
if i > 0:
p.x_range = scatter_plots[j].x_range
scatter_plots.append(p)
grid = gridplot(scatter_plots, ncols = len(dataset.columns))
show(grid)

matplotlib: how to define lines with text and mark lines joints?

I am trying to recreate the following:
Any comments will be appreciated. I want to imitate this picture actually, but I have 3 problems:
How to get known the joints of two lines and the turning points of a line? Could these specific points be calculated from analytical calculations? or matplotlib could find out them?
How can I draw the dashed vertical line segment just below the line joint?
How to paste text to the segments of the lines? Could matplotlib determine the convenient location to write text attached to the lines ? or I should determine the location myself ?
For example, I can only draw such kind as below, far less than required. Please help me to improve my picture.
My own picture, which need improvements:
Code so far with detail code as below:
# -*- coding: utf-8 -*
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import math
from pylab import *
c = 2.998*10**10
hp = 6.626*10**-27
hb = 1.055*10**-27
kb = 1.381*10**-16
g = 6.673*10**-8
me = 9.109*10**-28
mp = 1.673*10**-24
q = 4.803*10**-10
sigT = 6.652*10**-25
p = 2.5
E52 = 1000
epsB_r = 0.01
epse_r = 0.1
D28 = 1
n1 = 1.0
nu15 = 2*10**(-3)
r014 = 1
g42 = 5
delt12 =1
g4 = g42*10**2
E0 = E52*10**52
eta = g4
N0 = E0/(g4*mp*c**2)
p_td = 24*3600
p_txd = 3**(1./3)*2**(-4./3)*10**(52./3)*pi**(-1./3)*mp**(-1./3)*c**(-5./3)/p_td
txd = p_txd*n1**(-1./3)*eta**(-8./3)*E52**(1./3)
p_Fmax_r1 = 2**(1./2)*3**(-1)*pi**(-1./2)*me*mp**(1./2)*c**3*sigT*q**(-1)*p_txd**(-3./2)*10**(-56)
Fmax_r1 = lambda t : p_Fmax_r1*N0*eta**6*E52**(-1./2)*n1*epsB_r**(1./2)*D28**(-2)*t**(3./2)
p_Fmax_r2 = 2**(1./2)*3**(-1)*pi**(-1./2)*me*mp**(1./2)*c**3*sigT*q**(-1)*p_txd**(34./35)*10**(-56)
Fmax_r2 = lambda t : p_Fmax_r2*N0*epsB_r**(1./2)*D28**(-2)*t**(-34./35)*eta**(-62./105)*n1**(37./210)*E52**(34./105)
p_nuc_r1 = 2**(-13./2)*3**2*me*mp**(-3./2)*c**(-2)*sigT**(-2)*pi**(-1./2)*q*p_td**(-2)
p_nuc_r2 = 2**(-13./2)*3**2*pi**(-1./2)*me*mp**(-3./2)*c**(-2)*sigT**(-2)*q*p_txd**(-74./35)*p_td**(-2)
nuc_r1 = lambda t : p_nuc_r1*eta**(-4)*epsB_r**(-3./2)*n1**(-3./2)*t**(-2)
nuc_r2 = lambda t : p_nuc_r2*eta**(172./105)*t**(4./35)*n1**(-167./210)*epsB_r**(-3./2)*E52**(-74./105)
p_num_r1 = 2**(11./2)*7**(-2)*mp**(5./2)*me**(-3)*pi**(-1./2)*q*p_txd**(-6)
p_num_r2 = 2**(11./2)*7**(-2)*mp**(5./2)*me**(-3)*pi**(-1./2)*q*p_txd**(54./35)
num_r1 = lambda t : p_num_r1*eta**18*((p-2)/(p-1))**2*epse_r**2*epsB_r**(1./2)*n1**(5./2)*t**6*E52**(-2)
num_r2 = lambda t : p_num_r2*((p-2)/(p-1))**2*n1**(-1./70)*eta**(-74./35)*E52**(18./35)*t**(-54./35)*epse_r**2*epsB_r**(1./2)
def num_r_(t):
return num_r1(t) if t<txd else num_r2(t)
num_r = np.vectorize(num_r_)
def nuc_r_(t):
return nuc_r1(t) if t<txd else nuc_r2(t)
nuc_r = np.vectorize(nuc_r_)
def Fmax_r_(t):
return Fmax_r1(t) if t<txd else Fmax_r2(t)
Fmax_r = np.vectorize(Fmax_r_)
i= np.arange(-5,-2,0.05)
t = 10**i
dnum = [math.log10(mmm) for mmm in num_r(t)]
dnuc = [math.log10(j) for j in nuc_r(t)]
nu_obs = [math.log(nu15*10**15,10) for a in i]
plt.figure('God Bless: Observable Limit')
plt.title(r'$\nu_{obs}$ and $\nu_c$ and $\nu_m$''\nComparation')
plt.xlabel('Time: log t')
plt.ylabel(r'log $\nu$')
plt.axvline(math.log10(txd))
plt.plot(i,nu_obs,'--',linewidth=2,label=r'$\nu_{obs}$')
plt.plot(i,dnum,'-.',linewidth=2,label=r'$\nu_m$')
plt.plot(i,dnuc,linewidth=2,label=r'$\nu_c$')
plt.savefig("test4.eps", dpi=120,bbox_inches='tight')
plt.legend()
plt.show()
I just find a solution, not certain whether there would be some better solution.
I took reference here: Annotate some points
I assumed the solution like this :
1, We can calculate the joint point coordination of lines.
2, If we want to plot a segment of a vertical line, i.e. the segment below the joint point, we can choose two points to draw a short line. That does work!
3, Maybe we can only to find a location of the illustrative text, and attach the text to that place.
I add such phrases :
plot([math.log10(txd),math.log10(txd)],[4,math.log10(nuc_r(txd))], color ='blue', linewidth=2.5, linestyle="--")
scatter([math.log10(txd),],[math.log10(nuc_r(txd))], 50, color ='blue')
annotate(r'$sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',
xy=(math.log10(txd), math.log10(nuc_r(txd))), xycoords='data',
xytext=(+10, +30), textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
And got the result as :
A Better One

Categories

Resources