Share your best coding tips!!! #21
Replies: 14 comments 9 replies
-
I wrote a function to create a custom colorbar with your favorite colors
|
Beta Was this translation helpful? Give feedback.
-
Might be a simpler one, but when plotting data using (robust=True) has been incredibly useful for me in order to exclude weird data by only plotting the 95th percentile! |
Beta Was this translation helpful? Give feedback.
-
When loading in a dataset in xarray (and probably pandas too), you can drop loading in certain variables to be RAM friendly! data = xr.open_dataset(file, drop_variables=['variable_you_dont_need']) |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Having a look at all the nbextensions you can use in your notebooks - there are some really helpful ones. |
Beta Was this translation helpful? Give feedback.
-
This function requires the fig and the name, the rest are given so-called default values/strings.
|
Beta Was this translation helpful? Give feedback.
-
If you want to work in full screen mode
|
Beta Was this translation helpful? Give feedback.
-
If you want to increase the size on the labels and the numbers on your plot: Instead of 'Year' you can also write ds.long_name or ds.unit axs1.set_xlabel('Year', fontsize=20)
axs1.set_ylabel('Year', fontsize=20)
axs1.set_ylabel(ds.long_name, fontsize=20)
axs1.tick_params(axis="x", labelsize=20)
axs1.tick_params(axis="y", labelsize=20) To write a figure title and subfigure titles: fig.suptitle('Text', fontsize=30)
axs1.set_title(" Text" , fontsize=25) |
Beta Was this translation helpful? Give feedback.
-
Instead of putting function in util.py, that won't be updated in the notebook if you modify it, you can store functions in functions.ipynb, and run in your notebook %run functions.ipynb to use the functions, and run it again to get the update when you modify something in the functions.ipynb file. |
Beta Was this translation helpful? Give feedback.
-
I uploaded the notebooks from todays tutorial here :) Have fun! Sorry it got so long! |
Beta Was this translation helpful? Give feedback.
-
Correcting longitudes for plotting There can be a problem due to longitudes 'wrapping around' and going from -180 to +180, leads to issue with pcolormesh trying to make a quadrilateral with corners -180 and 180, Solution is to 'unwrap' longitudes : longitudePlt = np.unwrap(dataSet.longitude, period=360) |
Beta Was this translation helpful? Give feedback.
-
Calculate monthly climatology and anomaly |
Beta Was this translation helpful? Give feedback.
-
Hello go see my tuto for plotting :) |
Beta Was this translation helpful? Give feedback.
-
Plotting with multiple axis: def fix_ax(c, ax3):
ax3.yaxis.label.set_color(c)
ax3.tick_params(axis='y', colors=c, )
ax3.spines.right.set_color(c)
fi, ax1 = plt.subplots(1)
_ds.plot.hist(bins = np.logspace(-1,2), ax=ax1, alpha=.5)
ax1.set_ylabel('axis 1')
## Second axis:
col = 'r'
ax2 = ax1.twinx()
p = _ds.plot.hist(bins = np.linspace(0,20), ax=ax2, color=col, alpha=.5)
fix_ax(col, ax2)
ax2.set_ylabel('axis 2')
## Third axis
col = 'm'
ax3 = ax1.twinx()
p = _ds.plot.hist(bins = np.linspace(2,10), ax=ax3, color =col, alpha=.5)
# Specify position of axis
ax3.spines.right.set_position(("axes", 1.18))
ax3.set_ylabel('axis 3')
fix_ax(col, ax3)
ax1.set_xlim([0,40]) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all! I've been reflecting today over how many good tips and tricks we are all learning and maybe we could share a bit and then we can go through what people have shared in the forum??
Please comment with your best tips :)
I'll start with a couple I realized are useful:
This tells jupyter to check if the file you are importing has changed, and load the changes. If not you can edit the file until you are blue, but the notebook won't load the change until you restart the kernel.
If you change the units of something, you can keep track by editing the notebooks. If for example you have an aerosol concentration in units of 'm-3', and you want it in 'cm-3', you may do:
ALSO USE E.G:
The if test here ensures that you don't accidentally do it twice by running the same code twice.
Beta Was this translation helpful? Give feedback.
All reactions