Keeping matplotlib axis formatting for future plots - python

I really think my google skills and matplotlib documentation readings skills are failing me...but I do not seem to be able to find an answer.
I am applying some formatting to some plots I am doing:
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
ax.grid(True, 'major', 'y', color='#D3D3D3')
ax.tick_params(axis='both', which='major', labelsize=12, labelcolor='#545454')
I would like this formatting to be applied to any future plots I do as well. Currently, as soon as I do a new plot, even of the same chart type and using the same data, the default formatting comes back.
I realise I can plot multiple charts in one figure, and I am doing this at times, but sometimes I only want to plot one chart at a time.
Is there a way to do this? Or is copy and paste my only solution?
Thanks for your time,
Ian.

Related

How to add to two images in one figure on each other with different colormaps in matplotlib?

In some cases, especially in scientific papers, authors add two images in one figure on each other with different colormaps. you can see an example in the below images. Can anyone guide me on how to use matplotlib to create these kinds of images?
You can create one plot area and then simply add lines graphs, barcharts, etc. etc. to the same plot area. For example:
fig, ax = plt.subplots(figsize=(15,8))
ax.plot(df_1.index, df_1['Column_Name'], linewidth=5, color='blue', label='Some_Label')
ax.legend(loc='upper right')
ax.plot(df_2.index, df_2['Column_Name'], label='Some_Label')
ax.legend(loc='upper right')
You can create graphs using matplotlib, pandas or seaborn - it just depends what you are looking to create. What I did above is create 2 line graphs on the same plotting area - but you can do the same with other types of graphs.

How do I use matplotlib to plot a single scatterplot

I am trying to make a single scatterplot for my online course using matplotlib, but sometimes multiple plots are showing up on the screen.
Here is the code:
plt.figure()
plt.scatter(x[:2], y[:2], s=100, c='red', label='Tall students')
plt.scatter(x[2:], y[2:], s=100, c='blue', label='Short students')
plt.show()
My problem is that if I run the code twice I get two images like this:
If I run it again, I get only a single plot.
Is there any way to make sure I get only 1 plot here?
plt.clf() will clear the current figure. plt.cla() will clear all subplots.

Matplotlib default minor ticks

Pretty simple, I want minor ticks on my subplot on the xaxis, but not the yaxis. I've tried all sorts (there seem to be hundreds of methods for ticks) but all seem to want me to specify the locations for the ticks, or use a TickLocator. I just want them on, and not to have to specify where (just like how the Major ticks work automatically).
Something like:
axis.set_minor_xticks_on()
or
axis.xaxis.set_minor_ticks()
or
axis.set_ticks(which='minor', True)
seems so simple but these don't work and I can't seem to find an equivalent. Surely there is one? I don't want labels on the minor ticks.
Using Python 3.5.2 and Matplotlib 1.5.3
I managed to achieve what I wanted to achieve with:
ax.minorticks_on()
ax.tick_params(axis='x', which='minor', direction='out')

Matplotlib: Intelligent figure scale / legend location

Some code gives me the following matplotlib figure:
Unfortunately, the figure size is fixed and hence on the top right, the legend and the lines overlap. Is there any way to have the legend not stack on top of the lines?
I am aware that legend allows ax2.legend(loc=0), where 0 will put it into the "best" location. However, with two y axis as here, this will stack both legends on top of each other - not really the best allocation.
My next best try would be to "scale up" the figure, as manually done with an interactive graph, where I have only scaled up both axis:
Doing this with the "real" figure scale requires iterated "trying numbers and checking how far it goes" procedure - which may need to be redone if the graph changes. Is there any way of having matplotlib compute the scale "intelligently"?
If the best location plt.legend(loc='best') fails, try putting the legend outside of the plot:
plt.legend(loc='upper left', bbox_to_anchor=(1.02, 1), borderaxespad=0)
You can scale only legend, not the whole plot. Link here
More on legends here and also here.

Empty python plot

I'm running a script remotely on a cluster to generate a scatter plot. I wish to save the plot, but I don't want the plot to be display or a window to come up (as when you execute plt.show() ).
My saved plots are always empty. This is the code that I'm using (below). Any tips would be very helpful. Thanks!
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([-1,maxX+1])
ax.set_ylim([0,maxY+1])
ax.set_xlabel('Comparison number (n)', fontsize=18, fontweight='bold')
ax.set_ylabel('Normalized cross correlation score', fontsize=18, fontweight='bold')
ax.scatter(xaxis,yaxis)
plt.savefig('testfig.png')
In order to use avoid showing plot windows (i.e. to do off-screen rendering) you probably want to use a different matplotlib backend.
Before any matplotlib import statements, add
import matplotlib
matplotlib.use('Agg')
and subsequent calls to matplotlib will not show any plot windows.
If your plot file shows an empty axis, then the problem lies in the plotting arguments as calling plot with empty arguments creates an empty axis.

Categories