Matplotlib: xticks labels not showing - python-2.7

I'm new to matplotlib and I'm having a small problem. I'm trying to make 3 plots stacked on top of each other, sharing the x axis and with 2 different y axis.
This is my current code
import numpy as np
import matplotlib.pyplot as plt
periods = range(1,13)
n0 = [4.2, 6.7, 10.6, 51.3, 339, 45.6, 56.3, 112.9, 182.7, 185.7, 126.2, 25.39]
alp = [2.12, 2.14, 2.19, 2.35, 2.54, 2.33, 2.34, 2.43, 2.45, 2.46, 2.466, 2.249]
B = [0.045, 0.041, 0.04, 0.04, 0.057, 0.048, 0.044, 0.057, 0.054, 0.065, 0.06, 0.045]
emin = [166, 201.9, 215, 270.7, 351.8, 263.7, 302.2, 323.6, 328.7, 346.1, 279.5, 259.8]
emax = [21806, 28407, 5706, 22087, 17978, 11699, 19440, 17988, 26938, 14812, 14195, 26121]
eq = [7.8, 11.8, 13.3, 15.2, 8.87, 10.5, 13.8, 7.6, 11.5, 7.4, 6.4, 13.5]
f, (ax1, ax2, ax3) = plt.subplots(3, sharex = True)
ax1.scatter(periods, emin, c="k")
ax1.set_ylabel(r"$E_{min}")
ax1.yaxis.set_ticks(range(160, 380, 40))
ax4 = ax1.twinx()
ax4.scatter(periods, n0, c="r")
ax4.set_ylabel(r"$N_0$", color = 'r')
ax4.tick_params(colors = 'r')
ax4.yaxis.set_ticks(np.arange(20, 340, 50))
ax2.scatter(periods, emax, c="k")
ax2.set_ylabel(r"$E_{max}$")
ax2.yaxis.set_ticks(np.arange(5000, 30000, 5000))
ax5 = ax2.twinx()
ax5.scatter(periods, alpha, c="r")
ax5.set_ylabel("alp", color = 'r')
ax5.tick_params(colors = 'r')
ax5.yaxis.set_ticks(np.arange(2.1, 2.6, 0.1))
ax3.scatter(periods, eq, c="k")
ax3.set_ylabel("Eq")
ax3.yaxis.set_ticks(np.arange(6, 15, 2))
ax3.set_xlabel("Periods")
ax6 = ax3.twinx()
ax6.scatter(periods, B, c="r")
ax6.set_ylabel("B", color = 'r')
ax6.tick_params(colors = 'r')
ax6.yaxis.set_ticks(np.arange(0.02, 0.09, 0.02))
ax6.xaxis.set_ticks(range(1,13))
f.subplots_adjust(hspace = 0)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
plt.show()
But the x axis labels are not being shown. The label itself ("Periods") is fine, but the ticks aren't. I've tried to change the line
ax6.xaxis.set_ticks(range(1,13))
to every other number, but still doesn't work. Any help?
On a side note, how to rotate the y axis labels by 90 degrees so that they are horizontal? I've tried including rotation = 90 in the ylabel but it doesn't change (though other values work).
Thanks a lot
Edit: Forgot to add, if I erase the twinx axes it works fine

In order for your x ticks to appear you need to change the second to last line and set visible = True. In order to rotate the y labels by 90 degrees, use rotation = as you are doing, but set the rotation to 0. Doing this may make your labels and your ticks overlap. You can remove this by using the labelpad = in your ax.set_ylabel(). This is the spacing between the axis and the label.
f, (ax1, ax2, ax3) = plt.subplots(3, sharex = True)
ax1.scatter(periods, emin, c="k")
ax1.set_ylabel(r"$E_{min}$",rotation=0,labelpad=20)
ax1.yaxis.set_ticks(range(160, 380, 40))
ax4 = ax1.twinx()
ax4.scatter(periods, n0, c="r")
ax4.set_ylabel(r"$N_0$", color = 'r',rotation=0,labelpad=10)
ax4.tick_params(colors = 'r')
ax4.yaxis.set_ticks(np.arange(20, 340, 50))
ax2.scatter(periods, emax, c="k")
ax2.set_ylabel(r"$E_{max}$",rotation=0,labelpad=20)
ax2.yaxis.set_ticks(np.arange(5000, 30000, 5000))
ax5 = ax2.twinx()
ax5.scatter(periods, alp, c="r")
ax5.set_ylabel("alp", color = 'r',rotation=0,labelpad=15)
ax5.tick_params(colors = 'r')
ax5.yaxis.set_ticks(np.arange(2.1, 2.6, 0.1))
ax3.scatter(periods, eq, c="k")
ax3.set_ylabel("Eq",rotation=0,labelpad=20)
ax3.yaxis.set_ticks(np.arange(6, 15, 2))
ax3.set_xlabel("Periods")
ax6 = ax3.twinx()
ax6.scatter(periods, B, c="r")
ax6.set_ylabel("B", color = 'r',rotation=0,labelpad=10)
ax6.tick_params(colors = 'r')
ax6.yaxis.set_ticks(np.arange(0.02, 0.09, 0.02))
ax1.xaxis.set_ticks(np.arange(1, 13, 1))
f.subplots_adjust(hspace = 0,left=0.14,right=0.90)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=True)
plt.show()
This produces the following plot:
You may want to experiment with the labelpad to get the labels exactly where you want them as the length of your tick labels are not the same for each axis.

Related

Why doesn't an array made from a PIL draw.text() image show properly in Matplotlib?

I'd like to understand why, when I convert the PIL image imageRGB to a float array arrayRGB_f and use matplotlib's imshow() without a cmap it looks either black, or strange and unreadable, even though PIL's imageRGB.show() looks fine, and each of the individual r, g, b channels shown with cmap='gray' look okay as well.
I have workarounds, but I just don't understand why this happens.
matplotlib.__version__ returns '2.0.2' and I'm using MacOS with an Anaconda installation.
See this answer for more on the conversion of a ttf rendering to a 1bit.
fyi the output of the print statements are:
float64 (41, 101, 3)
int64 (41, 101, 3)
int64 (41, 101)
int64 (41, 101)
fontname = 'default'
imageRGB.show()
plt.imshow()
fontname = 'Arial Unicode.ttf'
imageRGB.show()
plt.imshow()
font = ImageFont.truetype(fontname, 20)
imageRGB.show()
plt.imshow()
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import matplotlib.pyplot as plt
# fontname = 'Arial Unicode.ttf'
fontname = 'default'
if fontname == 'default':
font = ImageFont.load_default()
else:
font = ImageFont.truetype(fontname, 12)
string = "Hello " + fontname[:6]
ww, hh = 101, 41
threshold = 80 # https://stackoverflow.com/a/47546095/3904031
imageRGB = Image.new('RGB', (ww, hh))
draw = ImageDraw.Draw(imageRGB)
image8bit = draw.text((10, 12), string, font=font,
fill=(255, 255, 255, 255)) # R, G, B alpha
image8bit = imageRGB.convert("L")
image1bit = image8bit.point(lambda x: 0 if x < threshold else 1, mode='1') # https://stackoverflow.com/a/47546095/3904031
arrayRGB = np.array(list(imageRGB.getdata())).reshape(hh, ww, 3)
arrayRGB_f = arrayRGB.astype(float)
array8bit = np.array(list(image8bit.getdata())).reshape(hh, ww)
array1bit = np.array(list(image1bit.getdata())).reshape(hh, ww)
for a in (arrayRGB_f, arrayRGB, array8bit, array1bit):
print a.dtype, a.shape
imageRGB.show()
if True:
plt.figure()
a = arrayRGB_f
plt.subplot(2, 2, 1)
plt.imshow(a) # , interpolation='nearest', cmap='gray',
for i in range(3):
plt.subplot(2, 2, 2+i)
plt.imshow(a[:, :, i], cmap='gray')
plt.suptitle('arrayRGB_f, fontname = ' + fontname)
plt.show()
I can't find an ideal duplicate so I'll post an answer.
As #ImportanceOfBeingErnest mentions when .imshow() is given an n x m x 3 or n x m x 4 array, it is expecting a normalized array between 0.0 and 1.0.
Best way to do this is:
arrayRGB_f = arrayRGB.astype(float)/255.
though this seems to work as well:
arrayRGB_f = arrayRGB.astype(float)
arrayRGB_f = arrayRGB_f / arrayRGB_f.max()
For longer discussions, see this and this.

Bringing one point to the front in matplotlib 3D scatter

I am trying to bring the star to the front in the following plot. I tried zorder, but it does not work. Do you have any suggestion?
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12,6))
ax = fig.gca(projection='3d')
ax.zaxis.get_major_formatter().set_useOffset(True)
#ax.zaxis.set_rotate_label(False)
#ax.yaxis.set_rotate_label(False)
#ax.xaxis.set_rotate_label(False)
ax.scatter(optim_index[np.argmin(optim_val)][0], optim_index[np.argmin(optim_val)][1], min(optim_val),
marker = '*', color = 'green', s=100, zorder=10, label = 'Optimal Solution')
ax.scatter(Plist, Vlist, Objlist, marker = '.', color = 'grey',label='Infeasible Solution')
ax.scatter(Feas_P, Feas_V, Objval, marker = '.', color = 'royalblue', label='Feasible Solution')
ax.view_init(30, 40)
ax.set_xlabel('Laser Power (W)', rotation = 6)
ax.set_ylabel('Scan Speed (mm/s)', rotation = -30)
ax.set_zlabel('Objective Value ($)', rotation = 92)
ax.legend(loc='upper center', bbox_to_anchor=(0.7, 0.8),
fancybox=True, shadow=False, ncol=1, scatterpoints = 1, fontsize=10)
plt.show() # Deactivate me to save the plot
#plt.savefig('ParamOptim.png', format='png') #dpi=900) #Activate me to save the plot

AttributeError: draw_artist can only be used after an initial draw which caches the render

My requirement is to plot the data in polar graph. However I need to keep polar graph in particular angle to looks like "V" shape and data need to plotted in between the particular angle.
In python I don't find a solution to keep the polar graph in particular angle, Example : Graph should be display in between -60 to 60 degree radius. To achieve that I have looked into couple of existing examples and creating required polar graph with FloatingSubplot functions. However I am hitting the issue , when we try to use along with function animation function with blit=True. Error message is displayed is "AttributeError: draw_artist can only be used after an initial draw which caches the render"
Here is my code.
#
import matplotlib
matplotlib.use('Qt4Agg')
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
import matplotlib.animation as animation
import mpl_toolkits.axisartist.floating_axes as floating_axes
from matplotlib.transforms import Affine2D
from matplotlib.projections import PolarAxes
from mpl_toolkits.axisartist import angle_helper
from mpl_toolkits.axisartist.grid_finder import MaxNLocator, DictFormatter
from mpl_toolkits.axisartist.floating_axes import GridHelperCurveLinear, FloatingSubplot
plt.close('all')
fig = plt.figure('Practice', dpi=100) # To set the fig title as pratice
ax1 = fig.add_subplot(2, 2, 1) # subplot for 1st plot
plt.ion()
ax1.grid(True)
def fractional_polar_axes(f, thlim=(0, 120), rlim=(0, 20), step=(30, 0.25),
thlabel='theta', rlabel='r', ticklabels=True, theta_offset=0, rlabels=None):
'''Return polar axes that adhere to desired theta (in deg) and r limits. steps for theta
and r are really just hints for the locators.'''
th0, th1 = thlim # deg
r0, r1 = rlim
thstep, rstep = step
tr_rotate = Affine2D().translate(theta_offset, 0)
# scale degrees to radians:
tr_scale = Affine2D().scale(np.pi / 180., 1.)
# pa = axes(polar="true") # Create a polar axis
pa = PolarAxes
tr = tr_rotate + tr_scale + pa.PolarTransform()
theta_grid_locator = angle_helper.LocatorDMS((th1 - th0) // thstep)
r_grid_locator = MaxNLocator((r1 - r0) // rstep)
theta_tick_formatter = angle_helper.FormatterDMS()
if rlabels:
rlabels = DictFormatter(rlabels)
grid_helper = GridHelperCurveLinear(tr,
extremes=(th0, th1, r0, r1),
grid_locator1=theta_grid_locator,
grid_locator2=r_grid_locator,
tick_formatter1=theta_tick_formatter,
tick_formatter2=rlabels)
a = FloatingSubplot(f, 222, grid_helper=grid_helper)
# a = Subplot(f,753, grid_helper=grid_helper)
# f.add_subplot(7,5,(3,34))
f.add_subplot(a)
# adjust x axis (theta):
print(a)
a.axis["bottom"].set_visible(False)
a.axis["top"].set_axis_direction("bottom") # tick direction
a.axis["top"].toggle(ticklabels=ticklabels, label=bool(thlabel))
a.axis["top"].major_ticklabels.set_axis_direction("top")
a.axis["top"].label.set_axis_direction("top")
a.axis["top"].major_ticklabels.set_pad(10)
# adjust y axis (r):
a.axis["left"].set_axis_direction("bottom") # tick direction
a.axis["right"].set_axis_direction("top") # tick direction
a.axis["left"].toggle(ticklabels=True, label=bool(rlabel))
# add labels:
a.axis["top"].label.set_text(thlabel)
a.axis["left"].label.set_text(rlabel)
# create a parasite axes whose transData is theta, r:
auxa = a.get_aux_axes(tr)
print(auxa)
# make aux_ax to have a clip path as in a?:
auxa.patch = a.patch
# this has a side effect that the patch is drawn twice, and possibly over some other
# artists. So, we decrease the zorder a bit to prevent this:
a.patch.zorder = -2
# add sector lines for both dimensions:
thticks = grid_helper.grid_info['lon_info'][0]
rticks = grid_helper.grid_info['lat_info'][0]
print(grid_helper.grid_info['lat_info'])
for th in thticks[1:-1]: # all but the first and last
auxa.plot([th, th], [r0, r1], ':', c='grey', zorder=-1, lw=0.5)
for ri, r in enumerate(rticks):
# plot first r line as axes border in solid black only if it isn't at r=0
if ri == 0 and r != 0:
ls, lw, color = 'solid', 1, 'black'
else:
ls, lw, color = 'dashed', 0.5, 'grey'
# From http://stackoverflow.com/a/19828753/2020363
auxa.add_artist(plt.Circle([0, 0], radius=r, ls=ls, lw=lw, color=color, fill=False,
transform=auxa.transData._b, zorder=-1))
return auxa
def animate(i):
global loopcount, th, r
th = th+.1
r = r+.1
datapoints.set_offsets(np.vstack((th,r)).T)
#print("in animate")
return datapoints,
if __name__ == '__main__':
r_locs = [0,5,10, 15, 20]
r_labels = ['0', '5', '10', '15', '20']
r_ticks = {loc: label for loc, label in zip(r_locs, r_labels)}
a1 = fractional_polar_axes(fig, thlim=(-60, 60), step=(20, 5),
theta_offset=90, rlabels=r_ticks)
th= 20
r=10
a1.scatter(th,r , c = 'r', alpha = 0.5, linewidths = '.2', s = 20) # plotting the line at thetha 20 and radius 10
datapoints = a1.scatter([], [], c='b', alpha = 0.5, linewidths = '.2', s = 20) # creating scatter line with given instruction,
ani = animation.FuncAnimation(fig, animate, frames=30, interval=20, blit=True)
plt.show(block=True)
#
"""
Above code is working perfectly fine with blit=False and also same solution working fine with line and scatter plotting in normal graph.
Please someone help me to resolve the issue.
"""

Matplotlib: plotting two legends outside of the axis makes it cutoff by the figure box

Task:
Plot a donut chart with two legends outside of the axis (first legend - on the right side with respect to the figure, second - on the bottom).
Problem:
When saving the figure, part of the 1st legend is cut off [especially when it contains a long text, see example below]
Desired result:
Make a tight layout of the figure by taking into consideration the dimensions of both legends.
Code:
import matplotlib.pyplot as plt
from pylab import *
ioff() # don't show figures
colors = [(102, 194, 165), (252, 141, 98), (141, 160, 203), (231, 138,195),
(166, 216, 84), (255, 217, 47), (171, 197, 233), (252, 205, 229)]
for icol in range(len(colors)):
red,green,blue = colors[icol]
colors[icol] = (red / 255., green / 255., blue / 255.)
fig = plt.figure(1, figsize=(8, 8))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
sizes_component_1 = [12, 23, 100, 46]
sizes_component_2 = [15, 30, 45, 10, 44, 45, 50, 70]
component_1 = 'exampleofalongtextthatiscutoff', '2', '3', '4'
component_2 = 'Unix', 'Mac', 'Windows7', 'Windows10', 'WindowsXP', 'Linux', 'FreeBSD', 'Android'
patches1, texts1, autotexts1 = ax.pie(sizes_component_1, radius=1, pctdistance=0.9, colors=colors, autopct='%1.1f%%', shadow=False, startangle=90)
patches2, texts2, autotexts2 = ax.pie(sizes_component_2, radius=0.8, pctdistance=0.6, colors=colors, autopct='%1.1f%%', shadow=False, startangle=90)
# To draw circular donuts
ax.axis('equal')
# Draw white circle
centre_circle = plt.Circle((0,0),0.6,color='black', fc='white')
ax.add_artist(centre_circle)
# Shrink current axis by 20%
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
lgd1=ax.legend(patches1,component_1, frameon=False, loc='center left', bbox_to_anchor=(1.0, 0.8), borderaxespad=0.1)
lgd2=ax.legend(patches2,component_2, frameon=False, loc='center left', ncol=len(patches2)/2, bbox_to_anchor=(0.0, -0.005), borderaxespad=0)
ax_elem = ax.add_artist(lgd1)
fig.suptitle('Title', fontsize=16)
fig.savefig('donut.png',bbox_extra_artists=(lgd1,lgd2,), bbox_inches='tight')
plt.gcf().clear() # clears buffer
This issue is come with pie chart: https://github.com/matplotlib/matplotlib/issues/4251
And it is not fixed.

How to add a time control panel to a FuncAnimation from matplotlib

I am currently using matplotlib.animation.FuncAnimation() to display an animation of my work, on a figure.
It is working very well, and I understand the arguments I am using ( interval, time range , ...) However, I was wondering if there was a way to implement (maybe directly to the figure) a panel containing the animation, a scroll-bar or whatever, which allows me to :
Move forward or backwards quickly to the time zone of interest.
Show at what point of the animation I am ( 10%, then 20%,...).
Basically, is a way to control the animation in python on the figure like the way I would control it as a video file played by a video player?
If needed, this is what looks like the code for this animation :
def init():
im1.set_data(XYslice[0, :, :])
im2.set_data(XZslice[0, Nplans/2:, :])
return([im1, im2])
def animate(t):
im1.set_data(XYslice[t, :, :])
im2.set_data(XZslice[t, Nplans/2:, :])
return [im1, im2]
anim = animation.FuncAnimation(fig, animate, np.arange(Ntime), interval=200,
blit=True, init_func=init, repeat=True)
What you are talking about is a GUI. The simplest example uses the matplotlib inbuilt widgets:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.mlab import bivariate_normal
from matplotlib.widgets import Slider, Button
#Setup figure and data
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)
delta = 0.5
t = np.arange(0.0, 100.0, 0.1)
x = np.arange(-3.0, 4.001, delta)
y = np.arange(-4.0, 3.001, delta)
X, Y = np.meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = (Z1 - Z2) * 5.
cmap = plt.cm.rainbow
im = ax.pcolormesh(X, Y, Z, cmap=cmap)
fig.colorbar(im)
axcolor = 'lightgoldenrodyellow'
axtime = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
stime = Slider(axtime, 'Time', 0.0, 100.0, valinit=50.0)
#Routines to reset and update sliding bar
def reset(event):
stime.reset()
def update(val):
time = stime.val/10.
Z = (Z1 - Z2) * time
im.set_array(Z.ravel())
fig.canvas.draw()
#Bind sliding bar and reset button
stime.on_changed(update)
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
button.on_clicked(reset)
plt.show()
This should be a start. If you want it to look better (and add more functionality) then you need to go to a GUI framework like wxpython, check out this example.
An example which is more inline with your data-structure would go as follows:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.mlab import bivariate_normal
from matplotlib.widgets import Slider, Button
#Setup figure and data
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)
delta = 0.5
t = np.linspace(0.0, 100.0, 256)
x = np.linspace(-4.0, 4.001, 512)
y = np.linspace(-4.0, 4.001, 512)
X, Y = np.meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
XZslice = np.zeros((256,512,512))
for i in range(t.shape[0]):
XZslice[i,:,:] = (Z1 - Z2) * t[i]/10.
cmap = plt.cm.rainbow
im = ax.pcolormesh(XZslice[128,:,:], cmap=cmap)
fig.colorbar(im)
axcolor = 'lightgoldenrodyellow'
axtime = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
stime = Slider(axtime, 'Time', 0.0, 100.0, valinit=50.0)
#Routines to reset and update sliding bar
def reset(event):
stime.reset()
def update(val):
time = int(stime.val/100.* 256)
im.set_array(XZslice[time,:,:].ravel())
fig.canvas.draw()
#Bind sliding bar and reset button
stime.on_changed(update)
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
button.on_clicked(reset)
plt.show()