org-arl / arlpy

ARL Python Tools
BSD 3-Clause "New" or "Revised" License
118 stars 37 forks source link

Axis label and tick label size to be passed as parameters to plot_env #60

Closed patel999jay closed 3 years ago

patel999jay commented 3 years ago

could we have control on the font size of the axis labels (x, y, color) and tick labels for 2D images plotted by plot_env by passing them as parameters ?

This seems weird to me as below method works fine with the plot but I think plot_env is the custom function but does that mean it didn't hold all the child property if you do plt.gcf() ? Please refer the below example for more details. May be it is something to do with bokeh.

import arlpy.uwapm as pm
import arlpy.plot as plt
from bokeh.plotting import figure, show
import numpy as np

env = pm.create_env2d()
surface = np.array([[r, 0.5+0.5*np.sin(2*np.pi*0.005*r)] for r in np.linspace(0,1000,1001)])
env['surface'] = surface

# This works fine as expected.
plt.figure(title='Demo 1', width=500)
plt.plot([0,10], [0,10],thickness=5, hold=True)
p = plt.gcf()
p.axis.major_tick_line_width = 10
p.xaxis.major_label_text_font_size = "25pt"
p.yaxis.major_label_text_font_size = "25pt"
show(p)

image

# can we have access to all child property of figure ?
plt.figure(title='Underwater Enviornment', width=900)
pm.plot_env(env,surface_color='blue', thickness=10, hold=True)
p = plt.gcf()
p.axis.major_tick_line_width = 10
p.xaxis.major_label_text_font_size = "25pt"
p.yaxis.major_label_text_font_size = "25pt"
show(p)

This won't let you set the major_tick_line_width and throw and an error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-30-f4abdc2e1059> in <module>
      4 pm.plot_env(env,surface_color='blue', thickness=10, hold=True)
      5 p = plt.gcf()
----> 6 p.axis.major_tick_line_width = 10
      7 p.xaxis.major_label_text_font_size = "25pt"
      8 p.yaxis.major_label_text_font_size = "25pt"

AttributeError: 'NoneType' object has no attribute 'axis'

image

mchitre commented 3 years ago

I'd avoid using Bokeh functions directly on the plot, as the arlpy.plot internal state is not updated when you show() a plot held by arlpy. The right way to show the held plot would be using plt.hold(False), as shown below:

import arlpy.uwapm as pm
import arlpy.plot as plt
import numpy as np

env = pm.create_env2d()
surface = np.array([[r, 0.5+0.5*np.sin(2*np.pi*0.005*r)] for r in np.linspace(0,1000,1001)])
env['surface'] = surface

plt.figure(title='Demo 1', width=500)
plt.plot([0,10], [0,10],thickness=5, hold=True)
p = plt.gcf()
p.axis.major_tick_line_width = 10
p.xaxis.major_label_text_font_size = "25pt"
p.yaxis.major_label_text_font_size = "25pt"
plt.hold(False)

The problem why your second code example doesn't work is that the hold option isn't supported by the plot_env() function. You can hold the plot first and then use plot_env() in the held plot. The following code does what you want:

plt.figure(title='Underwater Enviornment', width=900)
plt.hold(True)
pm.plot_env(env,surface_color='blue', thickness=10)
p = plt.gcf()
p.axis.major_tick_line_width = 10
p.xaxis.major_label_text_font_size = "25pt"
p.yaxis.major_label_text_font_size = "25pt"
plt.hold(False)
image