Matplotlib - setting tick positions on a specific subplot - python

I am trying something like this:
vol_grid = MultipleLocator(1000)
fig_vol = plt.subplot2grid((count, count), (i, 0), rowspan=1, colspan=count)
fig_vol.yaxis.set_major_locator(vol_grid)
but it has no effect. Any ideas?
My intention is to set the labels on the y-axis 1000 units apart, i.e. have labels at 1000, 2000, 3000, etc.

You haven't plotted anything, and thats is why the tick labels are not showing (the ylim defaults to (0,1), you can set vol_grid = MultipleLocator(0.1) to see that)
from matplotlib.ticker import MultipleLocator
import matplotlib.pyplot as plt
vol_grid = MultipleLocator(1000)
fig_vol = plt.subplot2grid((2, 2), (0, 0), rowspan=1, colspan=2)
fig_vol.yaxis.set_major_locator(vol_grid)
We can change the ylim or just plot some data:
fig_vol.set_ylim(1000, 9000)
#plt.plot([1,2,3], [1000, 2000, 5000])

Related

Changing axis ticks in Matplotlib with multiple connected Boxplots

I am plotting a convergence graph and to show deviations from the mean I am using connected boxplots:
For some reason Matplotlib forces ticks for each boxplot and I cannot seem to get them removed. My code for the current plot looks something like this:
label = ["" for i in range(160)]
no_labels = int(np.floor(len(label)/20))
for i in range(no_labels):
label[i*20] = str(i*no_samples/no_labels)
# Weird behaviour for the last label so adding it manually
label[-1] = no_samples
fig = plt.figure(figsize=(10,5))
ax = fig.add_axes([0,0,1,1])
ax.set_xlabel("Samples", labelpad=10)
ax.set_ylabel("Error (MSE)", labelpad=10)
ax.set_ylim(0, 0.11)
ax.boxplot(data, flierprops=flyprops, showcaps=False,
boxprops=colorprops, whiskerprops={'color' : 'tab:blue'},
labels=label, patch_artist=True)
I have tried multiple ways of manipulating axis ticks which are available in MPL.
1) Trying to let MPL do the work:
ax.xaxis.set_major_locator(MultipleLocator(20))
2) Trying to set ticks manually: ax.set_xticks([list_of_ticks])
3) Tried a workaround
ax.xaxis.set_minor_locator(MultipleLocator(20))
# Removing major ticks, setting minor ticks
ax.xaxis.set_tick_params(which='major', size=0, width=2, direction='in')
ax.yaxis.set_tick_params(which='major', size=5, width=2, direction='in')
None of these seemed to work and I am unsure why. I think it may have something to do with my label variable but if I don't include it in this way MPL with include an axis lable for every entry which is a mess.
How can I set axis ticks once every 1000 entries in a connected boxplots figure?`
Edit: The input data is a numpy array of shape (15, 160) s.t. there are 160 boxplots plotted of 15 samples each. Example data for 5 boxplots of 3 samples each would look like:
np.random.rand(3,5)
>>> array([[0.05942481, 0.03408175, 0.84021109, 0.27531937, 0.62428798],
[0.24658313, 0.77910387, 0.2161348 , 0.39101172, 0.14038211],
[0.40694432, 0.22979738, 0.87056873, 0.788295 , 0.29337562]])
The main issue seems to be that the ticks need to be updated after drawing the main plot, never before.
(Having ax = fig.add_axes([0, 0, 1, 1]) is also quite unusual to work with. The standard way is fig, ax = plt.subplots(figsize=(10, 5)) which lets matplotlib a bit of flexibility for the whitespace around the plot.)
The code of the question has some missing variables and data, but the following toy example should create something similar:
import numpy as np
import matplotlib.pyplot as plt
no_samples = 8000
x = np.linspace(0, no_samples, 160)
no_labels = int(np.floor(len(x) / 20))
label = [f'{i * no_samples / no_labels:.0f}' for i in range(no_labels+1)]
fig = plt.figure(figsize=(10, 5))
ax = fig.add_axes([0.1, 0.1, 0.85, 0.85])
N = 100
data = np.random.normal(np.tile(100 / (x+1000), N), 0.001).reshape(N, -1)
flyprops = {'markersize':0.01}
colorprops = None
ax.boxplot(data, flierprops=flyprops, showcaps=False,
boxprops=colorprops, whiskerprops={'color': 'tab:blue'},
patch_artist=True)
ax.set_xlabel("Samples", labelpad=10)
ax.set_ylabel("Error (MSE)", labelpad=10)
ax.set_ylim(0, 0.11)
ax.set_xticks(range(0, len(x)+1, 20))
ax.set_xticklabels(label)
plt.show()
Here is an example of setting the tick marks:
import matplotlib.pyplot as plt
import numpy as np
data=np.random.rand(3,50)
fig = plt.figure(figsize=(10,5))
ax = fig.add_axes([0,0,1,1])
ax.set_xlabel("Samples", labelpad=10)
ax.set_ylabel("Error (MSE)", labelpad=10)
ax.boxplot(data,
showcaps=False,
whiskerprops={'color' : 'tab:blue'},
patch_artist=True
)
plt.xticks([10, 20, 30, 40, 50],
["10", "20", "30", "40", "50"])
EDIT:
You can also avoid messing with strings and set the marks like this:
N=50
plt.xticks(np.linspace(0, N, num=6), np.linspace(0, N, num=6))
See here and this example.
Simple ticks can be acheived in a similar mannar as here (note data as transposed numpy array) using
import numpy as np
import matplotlib.pyplot as plt
data = np.array([ np.random.rand(100) for i in range(3) ]).T
plt.boxplot(data)
plt.xticks([1, 2, 3], ['mon', 'tue', 'wed'])

Python plot 3 variables data on same axis?

We conduct experiments, our oscilloscope gives all plots on same screen though each variables is different in magnitude. Is it possible to achive same in the python using the experimental data?
My present code and output:
import random
x = [i for i in range(1,11,1)]
y1 = random.sample(range(100, 1000), 10)
y2 = random.sample(range(0, 10), 10)
plt.plot(x,y1,'-r')
plt.plot(x,y2,'-g')
plt.legend(['y1','y2'])
plt.show()
There is a pretty simple solution to that just use subplots
import random
import matplotlib.pyplot as plt
x = [i for i in range(1,11,1)]
y1 = random.sample(range(100, 1000), 10)
y2 = random.sample(range(0, 10), 10)
ax1 = plt.subplot(211)
plt.plot(x,y1,'-r')
ax2 = plt.subplot(212,sharex=ax1)
plt.plot(x,y2,'-g')
ax1.get_shared_x_axes().join(ax1, ax2)
#make x axis on upper invisible
plt.setp(ax1.get_xaxis(), visible=False)
ax1.legend(['y1'])
ax2.legend(['y2'])
plt.show()
Looks like this
You can remove the bottom-border from the upper subplot and the upper border from the lower subplot with this:
ax1.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
GridSpec might help you to remove margins, however I gues there should be a simpler way to remove the distance between two subplots

Replicate Log10 Scaling with Matplotlib

I'm trying to recreate a plot that has the y-axis styled as so:
But can't seem to figure out how to get the axis breaks and labels lined up how I want them. I am currently doing this in my code:
# plot lines
for key, group in grouped:
plt.plot(group.x * 950, np.log10(group.y), label=key)
# plot points
exp_group = exp_data.groupby('Experiment')
for key, group in exp_group:
plt.plot(group.x, np.log10(group.y), label=key, marker='o')
plt.yticks(np.arange(-3, 3), label=10.0**np.arange(-3,3))
plt.show()
A solution is to use plt.yticks:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 1000)
y = np.exp(x) - x**3 + x**2 - x**(1/2)
plt.figure()
plt.semilogy(x, y)
locs, labels = plt.yticks()
print(locs)
print(labels)
lst_10 = [1, 10, 100, 1000, 10000]
plt.yticks(lst_10, lst_10)
plt.title('Different label on y-axis')
plt.figure()
plt.semilogy(x, y)
plt.title('Default label')
plt.show()
The function plt.yticks takes two arguments, the locations and the labels. I want the labels in the locations 1, 10, 100, 1000, 10000; I want the label (in location 1) to have a label 1, the label (in localtion 10) to have a label 10, and so on. I have also used plt.semilogy to get that semi-log axis.

change position of top major x-ticks as function of bottom axis

I want to something similar to How to add a second x-axis in matplotlib, i.e. have a top x-axis that displays a wavelength and a bottom axis that displays the corresponding frequency.
Reproducing linked example gives me a plot that looks like this:
This plot was produced with:
#setting up the plot
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.gridspec as gridspec
fig = plt.figure()
fig.tight_layout()
ax = plt.subplot()
#Here it gets interesting!
def tick_function(X):
c = 299792458
V = c/X
V = V*1e6
V = np.round(V,0)
V[2] = 3000
V = V.astype(int)
return(V)
ax = plt.subplot()
ax_top = ax.twiny()
ax.set_xscale("log", nonposx='clip')
ax.set_yscale("log", nonposy='clip')
ax_top.set_xscale("log", nonposx='clip')
ax.set_xlim([8e10,5e14])
ax.set_ylim([5e33,2e36])
axTicks = ax.get_xticks()
ax_top_Ticks = axTicks
ax_top.set_xticks(ax_top_Ticks)
ax_top.set_xlim(ax.get_xlim())
ax_top.set_xbound(ax.get_xbound())
ax_top.set_xticklabels(tick_function(ax_top_Ticks))
Now, rather than plotting the top major x-ticks at the position of the bottom major x-axis, I'd like to have them shifted.
I.e., I would like to have the top major x-ticks at positions 1000, 100, 10, 1 and the minor ticks shifted accordingly.
This is what I'd like it too look like:
I found this plot, that's what I want!
http://inspirehep.net/record/877424/files/fig2.png
Note, since lambda=c/f and ax & ax_top are logarithmic the spacing of the minor ticks has to be inverted to!
The trick is to choose the wavelengths you want and convert them to frequencies. Then use those frequencies as positions for the upper ticks.
#setting up the plot
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure()
ax = plt.subplot()
def conversion_freq_lam(inp):
c = 299792458
outp = c/inp
outp = outp.astype(int)
return outp
#ax = plt.subplot(gs1[0])
ax = plt.subplot(111)
ax_top = ax.twiny()
ax.set_xscale("log", nonposx='clip')
ax.set_yscale("log", nonposy='clip')
ax_top.set_xscale("log", nonposx='clip')
ax.set_xlim([8e10,5e14])
ax.set_ylim([5e33,2e36])
goal_lambdas = np.array([100000, 10000, 1000, 100, 10, 1, 0.1, 0.01])
goal_freqs = conversion_freq_lam(goal_lambdas)
ax_top_Ticks = goal_freqs * 1e6 # magic factor 1e6 from your attempt. Units?
ax_top.set_xticks(ax_top_Ticks)
ax_top.set_xlim(ax.get_xlim())
ax_top.set_xbound(ax.get_xbound())
ax_top.set_xticklabels(goal_lambdas)
plt.savefig('test_2axes.png')
This produces the following plot:
The magic number 1e6 used as a scaling factor I took from your question. I assume it is caused by the units of the axis.
Edit:
To have correctly spaced minor ticks at the top axis (for example at 2, 3, 4, ..., 20, 30, 40, 50, ...) add the following code block:
def find_minor_vals(goals):
minors = []
factors = np.arange(2, 10, 1)
for val in goals:
minors.extend(list(val * factors))
print minors
return np.array(minors)
goal_lambdas_minor = find_minor_vals(goal_lambdas)
goal_freqs_minor = conversion_freq_lam(goal_lambdas_minor) * 1e6
minor_locator = FixedLocator(goal_freqs_minor)
ax_top.xaxis.set_minor_locator(minor_locator)
Which results in the following picture:

Matplotlib: Broken_barh plots with same height

I'm working with broken_barh plots. Is there any way to get a fixed height of a single broken_barh? The image should get bigger vertically, but proportions should stay the same.
Here is a simple example.
import matplotlib.pyplot as plt
import matplotlib as mlp
fig = plt.figure()
ax = fig.add_subplot(111)
broken_barh(self, xranges, yrange, **kwargs)
ax.broken_barh([(110, 30), (150, 10)], (0, 10), facecolors='blue')
ax.broken_barh([(10, 50), (100, 20), (130, 10)] , (10, 10),
facecolors=('red', 'yellow', 'green'))
ax.broken_barh([(50, 30), (85, 10)], (20, 10), facecolors='black')
ax.set_xlim(0,200)
ax.set_xlabel('seconds since start')
ax.set_yticks([0,10,20])
ax.set_yticklabels(['Bill', 'Jim', 'Jeff'])
ax.grid(True)
plt.savefig('broken_barh_example.png', bbox_inches='tight')
plt.show()
If I generate two plots, one with two broken_barh and the other with three, it looks like this:
with 2 broken_barh
http://imageshack.us/a/img195/747/brokenbarhexample2.png
with 3 broken_barh
http://img341.imageshack.us/img341/5650/brokenbarhexamplenoyran.png
The render fits everything into the available space. If you want the size of the figure to grow as you add more rows, you can do it by hand via
fig.set_size_inches(w, h * num_rows, forward=True)
to force a fixed bar height.
(doc)

Categories