Remove padding in Matplotlib - matplotlib

I use plt.subplots to plot a list of images.
Here is my code:
m, n, c = output.shape
fig, ax = plt.subplots(nrows=4, ncols=1)
fig.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)
for i in range(4):
ax[i].imshow(output[:,:,i].reshape(m, n, 1), cmap='gray', vmin=0, vmax=1)
ax[i].axes.get_xaxis().set_visible(False)
ax[i].axes.get_yaxis().set_visible(False)
fig.set_facecolor('red')
plt.show()
And this is the result:
How can I remove the red space on the left and right of the figure?

Related

How to adjust the legend not to overlap in matplotlib?

I want to draw a line&bar chart by Matplotlib, it seems the legend of chart will overlap on upper left part.
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
plt.figure(figsize=(7,5), dpi=100)
x = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','>30']
y = [0.0964,0.1606,0.1651,0.1395,0.1105,0.0829,0.0603,0.0436,0.0328,0.0245,0.0183,0.0135,0.0105,0.0082,0.0061,0.0052,0.0041,0.0030,0.0024,0.0022,0.0013,0.0012,0.0010,0.0009,0.0009,0.0007,0.0006,0.0004,0.0005,0.0002,0.0023]
z = [11541,19234,19776,16704,13237,9931,7215,5218,3931,2936,2194,1619,1262,984,735,625,496,357,282,269,161,146,124,108,104,78,69,50,62,29,274]
plt.bar(x=x, height=z, label='Vehicles Num', color='Blue', alpha=0.7, width=0.5)
# plt.legend(loc="upper left")
plt.legend(loc="upper right")
plt.title("Frequency Distribution of Hood Ajar/Closed Events",size=18)
plt.xlabel("Frequency",size=15)
plt.ylabel("Number of Vehicles",size=15)
plt.yticks(size = 11)
plt.xticks(size = 11)
ax2 = plt.twinx()
ax2.set_ylabel("Percentage",size=15)
ax2.plot(y)
ax2.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))
plt.plot(x, y, "r", marker='.', c='r', ms=8, linewidth='1', label="Percentage")
plt.legend(loc="upper right")
# for a, b in zip(x, y):
# plt.text(a, b, str(b*100)+'%', ha='center', va='bottom', fontsize=13)
#plt.savefig('cx483_2.jpg')
plt.show()
What can I do to make my legend more harmonious? Thanks a lot.

Is there a way to label each wedge of pie chart in this grid?

I want to have multiple pie charts in a grid.
Each pie chart will have a different number of wedges, values, and labels.
The code below shows multiple labels in one pie chart.
Is there a way to label each wedge of pie-charts in this grid?
import matplotlib.pyplot as plt
import numpy as np
def heatmap_with_circles(data_array,row_labels,column_labels,ax=None, cmap=None, norm=None, cbar_kw={}, cbarlabel="", **kwargs):
for row_index, row in enumerate(row_labels,0):
for column_index, column in enumerate(column_labels,0):
print('row_index: %d column_index: %d' %(row_index,column_index))
if row_index==0 and column_index==0:
colors=['indianred','orange','gray']
values=[10,20,30]
else:
values=[45,20,38]
colors=['pink','violet','green']
wedges, text = plt.pie(values,labels=['0', '2', '3'],labeldistance = 0.25,colors=colors)
print('len(wedges):%d wedges: %s, text: %s' %(len(wedges), wedges, text))
radius = 0.45
[w.set_center((column_index,row_index)) for w in wedges]
[w.set_radius(radius) for w in wedges]
# We want to show all ticks...
ax.set_xticks(np.arange(data_array.shape[1]))
ax.set_yticks(np.arange(data_array.shape[0]))
fontsize=10
ax.set_xticklabels(column_labels, fontsize=fontsize)
ax.set_yticklabels(row_labels, fontsize=fontsize)
#X axis labels at top
ax.tick_params(top=True, bottom=False,labeltop=True, labelbottom=False,pad=5)
plt.setp(ax.get_xticklabels(), rotation=55, ha="left", rotation_mode="anchor")
# We want to show all ticks...
ax.set_xticks(np.arange(data_array.shape[1]+1)-.5, minor=True)
ax.set_yticks(np.arange(data_array.shape[0]+1)-.5, minor=True)
ax.grid(which="minor", color="black", linestyle='-', linewidth=2)
ax.tick_params(which="minor", bottom=False, left=False)
data_array=np.random.rand(3,4)
row_labels=['Row1', 'Row2', 'Row3']
column_labels=['Column1', 'Column2', 'Column3','Column4']
fig, ax = plt.subplots(figsize=(1.9*len(column_labels),1.2*len(row_labels)))
ax.set_aspect(1.0)
ax.set_facecolor('white')
heatmap_with_circles(data_array,row_labels,column_labels, ax=ax)
plt.tight_layout()
plt.show()
After updating heatmap_with_circles
def heatmap_with_circles(data_array,row_labels,column_labels,ax=None, cmap=None, norm=None, cbar_kw={}, cbarlabel="", **kwargs):
labels = ['x', 'y', 'z']
for row_index, row in enumerate(row_labels,0):
for column_index, column in enumerate(column_labels,0):
print('row_index: %d column_index: %d' %(row_index,column_index))
if row_index==0 and column_index==0:
colors=['indianred','orange','gray']
values=[10,20,30]
else:
values=[45,20,38]
colors=['pink','violet','green']
# wedges, texts = plt.pie(values,labels=['0', '2', '3'],labeldistance = 0.45,colors=colors)
wedges, texts = plt.pie(values,labeldistance = 0.25,colors=colors)
print('text:%s len(wedges):%d wedges: %s' %(texts, len(wedges), wedges))
radius = 0.45
[w.set_center((column_index,row_index)) for w in wedges]
[w.set_radius(radius) for w in wedges]
[text.set_position((text.get_position()[0]+column_index,text.get_position()[1]+row_index)) for text in texts]
[text.set_text(labels[text_index]) for text_index, text in enumerate(texts,0)]
I got the following image :)
You could loop through the texts of each pie, get its xy position, add column_index and row_index, and set that as new position.
Some small changes to the existing code:
ax.grid(which="minor", ..., clip_on=False) to make sure the thick lines are shown completely, also near the border
ax.set_xlim(xmin=-0.5) to set the limits
import matplotlib.pyplot as plt
import numpy as np
def heatmap_with_circles(data_array, row_labels, column_labels, ax=None):
ax = ax or plt.gca()
for row_index, row in enumerate(row_labels, 0):
for column_index, column in enumerate(column_labels, 0):
colors = np.random.choice(['indianred', 'orange', 'gray', 'pink', 'violet', 'green'], 3, replace=False)
values = np.random.randint(10, 41, 3)
wedges, text = plt.pie(values, labels=['1', '2', '3'], labeldistance=0.25, colors=colors)
radius = 0.45
for w in wedges:
w.set_center((column_index, row_index))
w.set_radius(radius)
w.set_edgecolor('white')
# w.set_linewidth(1)
for t in text:
x, y = t.get_position()
t.set_position((x + column_index, y + row_index))
# We want to show all ticks...
ax.set_xticks(np.arange(data_array.shape[1]))
ax.set_yticks(np.arange(data_array.shape[0]))
fontsize = 10
ax.set_xticklabels(column_labels, fontsize=fontsize)
ax.set_yticklabels(row_labels, fontsize=fontsize)
# X axis labels at top
ax.tick_params(top=True, bottom=False, labeltop=True, labelbottom=False, pad=5)
plt.setp(ax.get_xticklabels(), rotation=55, ha="left", rotation_mode="anchor")
# We want to show all minor ticks...
ax.set_xticks(np.arange(data_array.shape[1] + 1) - .5, minor=True)
ax.set_yticks(np.arange(data_array.shape[0] + 1) - .5, minor=True)
ax.set_xlim(xmin=-.5)
ax.set_ylim(ymin=-.5)
ax.grid(which="minor", color="black", linestyle='-', linewidth=2, clip_on=False)
ax.tick_params(axis="both", which="both", length=0) # hide tick marks
data_array = np.random.rand(3, 4)
row_labels = ['Row1', 'Row2', 'Row3']
column_labels = ['Column1', 'Column2', 'Column3', 'Column4']
fig, ax = plt.subplots(figsize=(1.9 * len(column_labels), 1.2 * len(row_labels)))
ax.set_aspect(1.0)
ax.set_facecolor('white')
heatmap_with_circles(data_array, row_labels, column_labels, ax=ax)
plt.tight_layout()
plt.show()

Is there a way to plot pie chart in matplotlib heatmap?

I have a heatmap with several rows and columns.
Formerly, I was plotting a circle for each (row_index,column_index) and appending this circle to a circle_list. I was adding circle_list as a collection to the axes.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import PatchCollection
def heatmap_with_circles(data_array,row_labels,column_labels,ax=None, cmap=None, norm=None, cbar_kw={}, cbarlabel="", **kwargs):
circles=[]
for row_index, row in enumerate(row_labels):
for column_index, column in enumerate(column_labels):
circles.append(plt.Circle((row_index,column_index),radius=0.4))
col = PatchCollection(circles, array=data_array.flatten(), cmap=cmap, norm=norm)
ax.add_collection(col)
# We want to show all ticks...
ax.set_xticks(np.arange(data_array.shape[1]))
ax.set_yticks(np.arange(data_array.shape[0]))
fontsize=10
ax.set_xticklabels(column_labels, fontsize=fontsize)
ax.set_yticklabels(row_labels, fontsize=fontsize)
#X axis labels at top
ax.tick_params(top=True, bottom=False,labeltop=True, labelbottom=False,pad=5)
plt.setp(ax.get_xticklabels(), rotation=55, ha="left", rotation_mode="anchor")
# We want to show all ticks...
ax.set_xticks(np.arange(data_array.shape[1]+1)-.5, minor=True)
ax.set_yticks(np.arange(data_array.shape[0]+1)-.5, minor=True)
ax.grid(which="minor", color="black", linestyle='-', linewidth=3)
ax.tick_params(which="minor", bottom=False, left=False)
data_array=np.random.rand(3,4)
row_labels=['Row1', 'Row2', 'Row3']
column_labels=['Column1', 'Column2', 'Column3','Column4']
fig, ax = plt.subplots(figsize=(1.9*len(row_labels),1.2*len(column_labels)))
ax.set_aspect(1.0)
ax.set_facecolor('white')
heatmap_with_circles(data_array,row_labels,column_labels, ax=ax)
plt.tight_layout()
plt.show()
However, now I need to plot a pie chart instead of a circle.
And pie chart does not have (row_index,column_index) parameters.
Is there a way to plot pie chart in each cell of matplotlib heatmap?
Updating the for loop in heatmap_with_circles as follows:
for row_index, row in enumerate(row_labels,0):
for column_index, column in enumerate(column_labels,0):
wedges, _ = plt.pie([20, 10, 5])
radius = 0.45
[w.set_center((column_index,row_index)) for w in wedges]
[w.set_radius(radius) for w in wedges]
results in
You can access each wedge created by plt.pie individually and then use set_radius and set_position to rescale the different wedges.
wedges, _ = plt.pie([1,2,3])
x_position, y_position = 0, 0
radius = 0.2
[w.set_center((x_position,y_position)) for w in wedges]
[w.set_radius(radius) for w in wedges]
Edit:
On your code, in the for loop
for row_index, row in enumerate(row_labels):
for column_index, column in enumerate(column_labels):
wedges, _ = plt.pie([1,2,3])
[w.set_center((row_index,column_index)) for w in wedges]
[w.set_radius(0.4) for w in wedges]

interactive legend with twinx - matplotlib

i want to create a plot with two y-axis and interactive legend. I made a minimal "working" example based on: https://matplotlib.org/3.1.1/gallery/event_handling/legend_picking.html
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 5, 0.01)
y1 = 2*np.sin(2*np.pi*t)
y2 = 4*np.sin(2*np.pi*2*t)+1
fig, ax = plt.subplots()
ax.set_title('Click on legend line to toggle line on/off')
line1, = ax.plot(t, y1, lw=2, label='1 HZ')
ax2 = ax.twinx()
line2, = ax2.plot(t, y2, lw=2, label='2 HZ')
lines_twinx=[line1,line2]
lbl = [l.get_label() for l in lines_twinx]
leg=ax.legend(lines_twinx, lbl, loc="upper left",fontsize='xx-small', shadow=True)
leg.get_frame().set_alpha(0.4)
lined = dict()
for legline, origline in zip(leg.get_lines(), lines_twinx):
legline.set_picker(5) # 5 pts tolerance
lined[legline] = origline
def onpick(event):
# on the pick event, find the orig line corresponding to the
# legend proxy line, and toggle the visibility
legline = event.artist
origline = lined[legline]
vis = not origline.get_visible()
origline.set_visible(vis)
# Change the alpha on the line in the legend so we can see what lines
# have been toggled
if vis:
legline.set_alpha(1.0)
else:
legline.set_alpha(0.2)
fig.canvas.draw()
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()
plot-figure
Somehow the legend is not clickable. Anybody knows what to do?
Using fig.legend() instead of ax.legend() helped me
leg=fig.legend(lines_twinx, lbl, loc="upper left",fontsize='xx-small', shadow=True)
view here

Custom background sections for matplotlib figure

I would like to define colors sections (blue: [0-15000], green: [15000-23000], red[23000,]) that should be used for y-values. Is it somehow possible in matplotlib?
You can color regions on a matplotlib plot using collections.BrokenBarHCollection:
import matplotlib.pyplot as plt
import matplotlib.collections as collections
fig = plt.figure()
ax = fig.add_subplot(111)
# Plot your own data here
x = range(0, 30000)
y = range(0, 30000)
ax.plot(x, y)
xrange = [(0, 30000)]
yrange1 = (0, 15000)
yrange2 = (15000, 23000)
yrange3 = (23000, 30000)
c1 = collections.BrokenBarHCollection(xrange, yrange1, facecolor='blue', alpha=0.5)
c2 = collections.BrokenBarHCollection(xrange, yrange2, facecolor='green', alpha=0.5)
c3 = collections.BrokenBarHCollection(xrange, yrange3, facecolor='red', alpha=0.5)
ax.add_collection(c1)
ax.add_collection(c2)
ax.add_collection(c3)
plt.show()