Increasing the space between the plot and the title with matplotlib - python

I am using the following script to generate some plots. The problem is sometimes the scientific notation is overlapping with the title.
Is there a way to fix this like moving the plot a little bit down?
# init
u = {}
o = {}
# create figure
fig = plt.figure()
# x-Axis (timesteps)
i = np.array(i)
for key in urbs_values.keys():
# y-Axis (values)
u[key] = np.array(urbs_values[key])
o[key] = np.array(oemof_values[key])
# draw plots
plt.plot(i, u[key], label='urbs_'+str(key), linestyle='None', marker='x')
plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))
plt.plot(i, o[key], label='oemof_'+str(key), linestyle='None', marker='.')
plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))
# plot specs
plt.xlabel('Timesteps [h]')
plt.ylabel('Flow [MWh]')
plt.title(site+' '+name)
plt.grid(True)
plt.tight_layout(rect=[0,0,0.7,1])
plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0)
# plt.show()
Example:

You can change the position of the title by providing a value for the y parameter in plt.title(...), e.g., plt.title(site+' '+name, y=1.1).

You can edit the tittle position this way:
# plot specs
plt.xlabel('Timesteps [h]')
plt.ylabel('Flow [MWh]')
plt.title(site+' '+name)
ttl = plt.title
ttl.set_position([.5, 1.02])
plt.grid(True)
plt.tight_layout(rect=[0,0,0.7,1])
plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0)
# plt.show()
tuning the '1.02' should do the trick

Related

Plot less points (line plot) for matplot lib

Currently I have a plot with too many points, I want to avoid overlapping. Want to know how to reduce the amount of points in order to have a smoother line.
Plot Code
fig = plt.figure(1, figsize = (18,10)) # Figure size in inches (size_x, size_y)
ax = plt.axes()
min_val = prediction_intervals2[:, 0]
max_val = prediction_intervals2[:, 1]
true_values = y_test
predicted_values = PLS_Model1.predict(X_test)
plt.plot(min_val, label = "Min", color='blue')
plt.plot(max_val, label = "Max", color='red')
plt.plot(true_values, label = "y", color = "black")
plt.plot(predicted_values, label = "y\u0302", marker='o')
plt.title('Conformal Predictor Final Predictions')
plt.legend()
plt.show()
Current Plot
Desired Plot
Plot that I want
I was able to revise my code properly and came to the desired output by just selecting less data points, quite simple. Posted the answer just in case.
min_val_normal = plot_normalized_table[['Min']]
max_val_normal = plot_normalized_table[['Max']]
original_normal = plot_normalized_table[['Original Label']]
interval_normal = plot_normalized_table[['Interval Size']]
normal_predicted = predicted_values[0:50]
fig = plt.figure(1, figsize = (18,10)) # Figure size in inches (size_x, size_y)
ax = plt.axes()
#predicted_values = PLS_Model1.predict(X_test) #Predictions from test data (run at least once for the plot to work)
plt.plot(min_val_normal, label = "Min", color='blue')
plt.plot(max_val_normal, label = "Max", color='red')
plt.plot(original_normal, label = "y", color = "black")
plt.plot(normal_predicted, label = "y\u0302", marker='o', )
plt.title('Normalized Final Conformal Predictions')
plt.xlim([-1, 51])
plt.ylim([-1, 2])
plt.legend()
plt.show()

How do I remove negative X axis labels in a population pyramid?

I have a population pyramid but the x axis on one side is negative, is there a way to rename just the negative x axis so that it is positive?
# Draw Plot
plt.figure(figsize=(13,10), dpi= 80)
group_col = 't1_gender'
order_of_bars = df.agegroup.unique()[::-1]
colors = [plt.cm.Spectral(i/float(len(df[group_col].unique())-1)) for i in range(len(df[group_col].unique()))]
for c, group in zip(colors, df[group_col].unique()):
sns.barplot(x='count', y='agegroup', data=df.loc[df[group_col]==group, :], order=order_of_bars, color=c, label=group)
# Decorations
plt.xlabel("Number of calls")
plt.ylabel("Age group")
plt.yticks(fontsize=12)
plt.title("", fontsize=22)
plt.legend()
plt.savefig('images/figure2.png', dpi=300, facecolor=ax.get_facecolor(), transparent=True, pad_inches=0.0)
plt.show()
A possible solution is to get the current ticks using get_xticks() and then use the np.abs function to force the tick labels to be positive:
xticks = plt.gca().get_xticks().astype(np.int)
plt.xticks(xticks, labels=np.abs(xticks));
(add the code to the #decoration section)

pyplot barchart is plotted, but saved to empty file

I have a strange behaviour in my python code, where I plot a bar-chart and it will be showed in a window, but pressing the "save"-button does nothing and .savefig() creates an empty file with 0KB.
Even more strange, I have a for-loop before the barchart, which is creating several complicated plots and all of them are capable to be plotted and then saved, except the barchart, which is only plotted.
I do not receive any errors from the python-console.
This is my code from the plot-loop before the bar-chart:
for l in range(NumberOfResults):
plottitle=groupLimits[0][l+1]+' - Result Width and Probability Distribution of ' + str(numberCombinations) + ' possible Combinations'
fig=plt.figure(plottitle,figsize=(GetSystemMetrics(0)/96,GetSystemMetrics(1)/96),dpi=96)
plt.clf()
ax=fig.add_subplot(1,1,1)
ResultColumn=range(NumberOfInputs,NumberOfInputs+NumberOfResults)[l]
positions=range(len(groupResults))
#positions=list(map(int, np.array(groupLimits[1:])[:,0]))
#plt.grid(1)
y1=[]
x1=[]
for i in range(len(groupResults)):
y1.append(groupResults[i][:,ResultColumn])
x1.append([positions[i]]*len(groupResults[i]))
y=list(np.array(y1).flatten())
s=[]
for i in range(len(groupResults)):
smin=min(groupResults[i][:,ProbabilityColumn])
smax=max(groupResults[i][:,ProbabilityColumn])
s.append((groupResults[i][:,ProbabilityColumn]-smin)/(smax-smin)*25000)
plt.scatter(x1,y1,s=s,marker='_',color='cyan',alpha=0.45)
bp=plt.boxplot(y1, positions=positions, widths=0.5,
labels=list(np.array(groupLimits[1:])[:,0]),
patch_artist=True,whis=[0,100])
#plt.xlim(min(positions)*0.9,max(positions)*1.1)
ax.xaxis.set_tick_params(labelsize=24)
ax.xaxis.grid(True,'major',color='k', linestyle='-', linewidth=0.75)
ax.xaxis.grid(True,'minor',color='k', linestyle=':', linewidth=0.25)
ax.xaxis.set_minor_locator(MultipleLocator(50));
ax.yaxis.set_tick_params(labelsize=24)
ax.yaxis.grid(True,'major',color='k', linestyle='-', linewidth=0.25)
ax.yaxis.grid(True,'minor',color='k', linestyle=':', linewidth=0.25)
ax.yaxis.set_minor_locator(MultipleLocator(0.1));
ax.yaxis.set_ticks(np.arange(int(min(y)),int(max(y))*1.1,0.5))
## change outline color, fill color and linewidth of the boxes
for box in bp['boxes']:
# change outline color
box.set( color='blue', linewidth=3)
# change fill color
box.set( facecolor = 'None')
## change color and linewidth of the whiskers
for whisker in bp['whiskers']:
whisker.set(color='blue', linewidth=3)
## change color and linewidth of the caps
for cap in bp['caps']:
cap.set(color='blue', linewidth=3,alpha=0.8)
## change color and linewidth of the medians
for median in bp['medians']:
median.set(color='magenta', linewidth=3)
y2=[]
x2=[]
for i in range(1,len(groupResults)+1):
if groupLimits[i][l+1]=='-':
pass
else:
y2.append(float(groupLimits[i][l+1]))
if FailedCombinations[i-1][l] is None:
failureString = 'No Results below the Limit'
else:
failureString= str(len(FailedCombinations[i-1][l]))+' failed # ' + "{:.3g}".format(FailureProbability[i-1][l]*100) + '%'
plt.text(positions[i-1],float(groupLimits[i][l+1])*0.9,
failureString,fontsize=18,horizontalalignment='center',
bbox=dict(facecolor='yellow', edgecolor='black'))
x2.append(positions[i-1])
plt.plot(x2,y2,marker='x',color='red', mew=4,ms=15,linestyle='None')
blue_patch = mpatches.Patch(color='blue', label='Result width')
red_patch = mlines.Line2D([],[],marker='x',color='red', mew=4,ms=15,linestyle='None', label='Limit')
magenta_patch = mpatches.Patch(color='magenta', label='Median')
cyan_patch = mpatches.Patch(color='cyan', label='Probability of Result')
plt.legend(handles=[blue_patch, red_patch, magenta_patch, cyan_patch],
loc='upper left',ncol=1,fontsize=24)
plt.title(plottitle,fontsize=24)
plt.xlabel('Engine Rotation Speed [rpm]',fontsize=24)
plt.ylabel('Absolute Pressure [bar]',fontsize=24)
labelsize = 24
rcParams['xtick.labelsize'] = labelsize
rcParams['ytick.labelsize'] = labelsize
plt.savefig(plottitle)
plt.show()
In between I run some calculations to prepare data for the bar-chart plot. However, the code for the bar-chart is:
# Plot group-wise combined failures with all-combined failures at end
plottitle = 'Failed combinations per group - and engine-all combined - TOTAL combinations: ' + str(len(groupResults[0]))
plt.figure(plottitle,figsize=(GetSystemMetrics(0)/96,GetSystemMetrics(1)/96),dpi=96)
plt.clf()
ax=plt.subplot(1,1,1)
y=[]
ind = np.arange(len(groupFailureProbabilities))
for i in range(len(groupFailures)):
y.append(len(groupFailures[i]))
y=tuple(y)
for i in range(len(y)):
if y[i]==0:
pass
else:
string=str(y[i])+' failed # ' + "{:.3g}".format(groupFailureProbabilities[i]*100)+' %'
plt.text(ind[i],y[i]+100,
string,fontsize=18, horizontalalignment='center',
bbox=dict(facecolor='yellow', edgecolor='black'))
width = 0.35
y=list(y)
plt.bar(ind, y, align='center', color='b')
labels=list(np.array(groupLimits[1:])[:,0])
labels.append('engine \n complete')
ax.set_xticks(ind)
ax.set_xticklabels(tuple(labels))
ax.yaxis.set_tick_params(labelsize=24)
ax.yaxis.grid(True,'major',color='k', linestyle='-', linewidth=0.25)
ax.yaxis.grid(True,'minor',color='k', linestyle=':', linewidth=0.25)
ax.yaxis.set_ticks(np.arange(int(min(y)),int(max(y))*1.1,500))
plt.title(plottitle,fontsize=24)
plt.xlabel('Engine Rotation Speed [rpm]',fontsize=24)
plt.ylabel('Number of Failed Combinations',fontsize=24)
plt.savefig(plottitle)
plt.show()
What is the reason why I cannot save my bar-chart ?
It's kinda difficult to write the shortest code necessary to reproduce the problem, if I have no idea why it happened in the first place.
However, I managed to get it working with a rearrangement of the code lines:
plottitle = 'Failed combinations per group - TOTAL combinations ' + str(len(groupResults[0]))
plt.figure(plottitle,figsize=(GetSystemMetrics(0)/96,GetSystemMetrics(1)/96),dpi=96)
plt.clf()
ax=plt.subplot(1,1,1)
plt.bar(ind, y, align='center')
labels=list(np.array(groupLimits[1:])[:,0])
labels.append('engine \n complete')
ax.set_xticks(ind)
ax.set_xticklabels(tuple(labels))
ax.yaxis.set_tick_params(labelsize=24)
ax.yaxis.grid(True,'major',color='k', linestyle='-', linewidth=0.25)
ax.yaxis.grid(True,'minor',color='k', linestyle=':', linewidth=0.25)
ax.yaxis.set_ticks(np.arange(int(min(y)),int(max(y))*1.1,500))
plt.title(plottitle,fontsize=24)
plt.xlabel('Engine Rotation Speed [rpm]',fontsize=24)
plt.ylabel('Number of Failed Combinations',fontsize=24)
plt.savefig(plottitle+'.png')
plt.show()

Does anyone know how to get rid of the black 'y' axis to the left in Matplotlib plot?

After moving all of my 'y' axes to subplots I get an unwanted axis. It's the black one on the left. Does anyone know how to get rid of it? I'm sure it's getting plotted when I call the figure, however I'm not sure how to get rid of it.
def mpl_plot(self, plot_page, replot = 0): #Data stored in lists
if plot_page == 1: #Plot 1st Page
#plt0 = self.mplwidget.axes
fig = self.mplwidget.figure #Add a figure
if plot_page == 2: #Plot 2nd Page
#plt0 = self.mplwidget_2.axes
fig = self.mplwidget_2.figure #Add a figure
if plot_page == 3: #Plot 3rd Page
#plt0 = self.mplwidget_3.axes
fig = self.mplwidget_3.figure #Add a figure
#Clears Figure if data is roplotted
if replot == 1:
fig.clf()
par0 = fig.add_subplot(111)
par1 = fig.add_subplot(111)
par2 = fig.add_subplot(111)
#Add Axes
plt = par0.twinx()
ax1 = par1.twinx()
ax2 = par2.twinx()
impeller = str(self.comboBox_impellers.currentText()) #Get Impeller
fac_curves = self.mpl_factory_specs(impeller)
fac_lift = fac_curves[0]
fac_power = fac_curves[1]
fac_flow = fac_curves[2]
fac_eff = fac_curves[3]
fac_max_eff = fac_curves[4]
fac_max_eff_bpd = fac_curves[5]
fac_ranges = self.mpl_factory_ranges()
min_range = fac_ranges[0]
max_range = fac_ranges[1]
#Plot Chart
plt.hold(True)
plt.plot(fac_flow, fac_lift, 'b', linestyle = "dashed", linewidth = 1)
ax1.plot(fac_flow, fac_power, 'r', linestyle = "dashed", linewidth = 1)
ax2.plot(fac_flow, fac_eff, 'g', linestyle = "dashed", linewidth = 1)
#Move spines
ax2.spines["right"].set_position(("outward", 25))
self.make_patch_spines_invisible(ax2)
ax2.spines["right"].set_visible(True)
#Plot x axis minor tick marks
minorLocatorx = AutoMinorLocator()
ax1.xaxis.set_minor_locator(minorLocatorx)
ax1.tick_params(which='both', width= 0.5)
ax1.tick_params(which='major', length=7)
ax1.tick_params(which='minor', length=4, color='k')
#Plot y axis minor tick marks
minorLocatory = AutoMinorLocator()
plt.yaxis.set_minor_locator(minorLocatory)
plt.tick_params(which='both', width= 0.5)
plt.tick_params(which='major', length=7)
plt.tick_params(which='minor', length=4, color='k')
#Make Border of Chart White
fig.set_facecolor('white')
#Plot Grid
plt.grid(b=True, which='both', color='k', linestyle='-')
#set shaded Area
plt.axvspan(min_range, max_range, facecolor='#9BE2FA', alpha=0.5) #Yellow rectangular shaded area
#Set Vertical Lines
plt.axvline(fac_max_eff_bpd, color = '#69767A')
#BEP MARKER *** Can change marker style if needed
bep = fac_max_eff * 0.90 #bep is 90% of maximum efficiency point
bep_corrected = bep * 0.90 # We knock off another 10% to place the arrow correctly on chart
ax2.annotate('BEP', xy=(fac_max_eff_bpd, bep_corrected), xycoords='data', #Subtract 2.5 shows up correctly on chart
xytext=(-50, 30), textcoords='offset points',
bbox=dict(boxstyle="round", fc="0.8"),
arrowprops=dict(arrowstyle="-|>",
shrinkA=0, shrinkB=10,
connectionstyle="angle,angleA=0,angleB=90,rad=10"),
)
#Set Scales
plt.set_ylim(0,max(fac_lift) + (max(fac_lift) * 0.40)) #Pressure
#plt.set_xlim(0,max(fac_flow))
ax1.set_ylim(0,max(fac_power) + (max(fac_power) * 0.40)) #Power
ax2.set_ylim(0,max(fac_eff) + (max(fac_eff) * 0.40)) #Effiency
plt.yaxis.tick_left()
# Set Axes Colors
plt.tick_params(axis='y', colors='b')
ax1.tick_params(axis='y', colors='r')
ax2.tick_params(axis='y', colors='g')
# Set Chart Labels
plt.yaxis.set_label_position("left")
plt.set_xlabel("BPD")
plt.set_ylabel("Feet" , color = 'b')
#ax1.set_ylabel("BHP", color = 'r')
#ax1.set_ylabel("Effiency", color = 'g')
# Set tight layout
fig.set_tight_layout
# Since we moved Feet Axis to subplot, extra unneeded axis was created. This Removes it
# Refresh
fig.canvas.update()
fig.canvas.draw()
Well it looks like you have three y-axes, referencing the one you want to not be shown, you could try adding:
ax.yaxis.set_tick_params(labelsize=0, length=0, which='major')
to just make invisible the labels and ticks. I think it's ax2 you want gone?

Contour plot legend - Matplotlib

As the question says, I have a contour plot and I would like show a legend for if.
I'm using the contour plot style that uses:
dashed lines for negative levels
solid lines for positive values
I would like to have a legend for them (dashed == negative and solid == positive).
I tried the approaches found here and here. However, as can be seen below, this doesn't show the correct result.
# Draw the scalar field level curves
div_field = plt.contour(x, y, div_scalar_field, colors='white')
rot_field = plt.contour(x, y, rot_scalar_field, colors='lightgoldenrodyellow')
labels = ['Div Neg', 'Div Pos', 'Rot Neg', 'Rot Pos']
div_field.collections[0].set_label(labels[0])
div_field.collections[-1].set_label(labels[1])
rot_field.collections[0].set_label(labels[2])
rot_field.collections[-1].set_label(labels[3])
As I for the div scalar field I just have positive levels, I got two labels with the same line style.
I'm wondering how could I achieve what I want properly.
Thank you in advance.
I could solve this manually setting the legend (which I don't know if it's the best approach):
div_neg = plt.Line2D((0, 1), (0, 0), color='white', linestyle='--', linewidth=2)
div_pos = plt.Line2D((0, 1), (0, 0), color='white', linestyle='-', linewidth=2)
rot_neg = plt.Line2D((0, 1), (0, 0), color='lightgoldenrodyellow', linestyle='--', linewidth=2)
rot_pos = plt.Line2D((0, 1), (0, 0), color='lightgoldenrodyellow', linestyle='-', linewidth=2)
plt.legend([rot_max, div_neg, div_pos, rot_neg, rot_pos],
['Rot Max Points', 'Div Neg', 'Div Pos', 'Rot Neg', 'Rot Pos'])
Something like the following works for me - this complete hack is to use a labelled dummy point, fetch its colour, apply that to the contours and then just plot the legend in the usual way:
import matplotlib as plt
labels = ['div_field'] # etc.
dummy_position = [-1.0e3,-1.0e3] # Could automate
colors = []
for k in labels:
# Fetch colours via a dummy point
dummy_point = plt.plot(dummy_position[0],dummy_position[1], label = k)
c = dummy_point[-1].get_color()
colors.append(c)
# This is specific to your problem, but roughly:
div_field = plt.contour(x, y, div_scalar_field, colors=c)
# etc.
_=plt.legend()
plt.savefig('contours.pdf')
Hope that makes sense.

Categories