Put legend on a place of a subplot - python

I would like to put a legend on a place of a central subplot (and remove it).
I wrote this code:
import matplotlib.pylab as plt
import numpy as np
f, ax = plt.subplots(3,3)
x = np.linspace(0, 2. * np.pi, 1000)
y = np.sin(x)
for axis in ax.ravel():
axis.plot(x, y)
legend = axis.legend(loc='center')
plt.show()
I do not know how to hide a central plot. And why legend is not appear?
This link did not help http://matplotlib.org/1.3.0/examples/pylab_examples/legend_demo.html

There are several problems with your code. In your for loop, you are attempting to plot a legend on each axis (the loc="center" refers to the axis, not the figure), yet you have not given a plot label to represent in your legend.
You need to choose the central axis in your loop and only display a legend for this axis. This iteration of the loop should have no plot call either, if you don't want a line there. You can do this with a set of conditionals like I have done in the following code:
import matplotlib.pylab as plt
import numpy as np
f, ax = plt.subplots(3,3)
x = np.linspace(0, 2. * np.pi, 1000)
y = np.sin(x)
handles, labels = (0, 0)
for i, axis in enumerate(ax.ravel()):
if i == 4:
axis.set_axis_off()
legend = axis.legend(handles, labels, loc='center')
else:
axis.plot(x, y, label="sin(x)")
if i == 3:
handles, labels = axis.get_legend_handles_labels()
plt.show()
This gives me the following image:

Related

Overlapping y axis lable in matplotlib

I have these code here to create an xgboost feature importance plot with more than 40 variables :
plot_importance(xgb_model)
plt.show()
However, I got a plot with overlapping y-axis labels and it was hard to read. The figsize=() argument did not seem to work.
Is there a way to make this plot readable?
Definitely go with figsize. You can see that because if you interactively change the window size you observe that the ticks labels d on't overlap anymore.
You can also change the font properties, see https://stackoverflow.com/a/11386056/13636407.
import numpy as np
import matplotlib.pyplot as plt
def plot_sin(figsize):
x = np.linspace(0, 4 * np.pi)
y = np.sin(x)
fig, ax = plt.subplots(figsize=figsize)
ax.plot(x, y)
ax.set_yticks(np.arange(-1.15, 1.15, 0.05))
ax.set_title(f"{figsize = }")
plot_sin(figsize=(12, 4))
plot_sin(figsize=(12, 10))
plt.show()

matplotlib: labeling of curves

When I create a plot with many curves it would be convenient to be able to label each curve at the right where it ends.
The result of plt.legend produces too many similar colors and the legend is overlapping the plot.
As one can see in the example below the use of plt.legend is not very effective:
import numpy as np
from matplotlib import pyplot as plt
n=10
x = np.linspace(0,1, n)
for i in range(n):
y = np.linspace(x[i],x[i], n)
plt.plot(x, y, label=str(i))
plt.legend(loc='upper right')
plt.show()
If possible I would like to have something similar to this plot:
or this:
I would recommend the answer suggested in the comments, but another method that gives something similar to your first option (albeit without the exact placement of the legend markers matching the positions of the associated lines) is:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
n=10
x = np.linspace(0, 1, n)
labels = [str(i) for i in range(len(x))]
for i in range(n):
y = np.linspace(x[i], x[i], n)
ax.plot(x, y, label=labels[i])
h, _ = ax.get_legend_handles_labels()
# sort the legend handles/labels so they are in the same order as the data
hls = sorted(zip(x, h, labels), reverse=True)
ax.legend(
[ha[1] for ha in hls], # get handles
[la[2] for la in hls], # get labels
bbox_to_anchor=(1.04, 0, 0.1, 1), # set box outside of axes
loc="lower left",
labelspacing=1.6, # add space between labels
)
leg = ax.get_legend()
# expand the border of the legend
fontsize = fig.canvas.get_renderer().points_to_pixels(leg._fontsize)
pad = 2 * (leg.borderaxespad + leg.borderpad) * fontsize
leg._legend_box.set_height(leg.get_bbox_to_anchor().height - pad)
This is heavily reliant on the answers here and here.

Fix the plot size after displaying a matrix with spy

I have a matplotlib figure that am using embedded into a pyQT GUI, therefore I need to recycle the figure to display several resuls.
When I display a matrix using the spy function, I get what I spect:
However when I clear the figure and plot a series I get this:
Instead of this:
Which I get if I plot the series without displaying a matrix before.
So a script to reproduce the issue is:
from matplotlib.pyplot import figure, show
import numpy
fig = figure()
ax = fig.add_subplot(111)
mat = numpy.random.randn(20, 20)
# display the matrix
ax.spy(mat, markersize=5)
x = numpy.linspace(0, 1, 100)
y = x**2 + x - 5
ax.clear()
ax.plot(x, y)
I have also tried
ax.relim() # make sure all the data fits
ax.autoscale() # auto-scale
But it doesn't do anything noticeable.
plt.spy will automatically set the aspect ratio of the axes to 'equal' in order to ensure that the sparsity plot for a square matrix looks square. If the x-axis scale of your series is much larger than that of the y-axis, an equal aspect ratio will result in a very long and thin line plot.
To switch back to the 'default' mode where the aspect ratio is determined automatically you can call ax.set_aspect('auto'):
from matplotlib.pyplot import figure, show
import numpy
fig = figure()
ax = fig.add_subplot(111)
mat = numpy.random.randn(20, 20)
# display the matrix
ax.spy(mat, markersize=5)
x = numpy.linspace(0, 1, 100)
y = x**2 + x - 5
ax.clear()
ax.set_aspect('auto')
ax.plot(x, y)

How to plot contourf and my graph in the same figure

I have a figure showing the contourf plot and another showing a plot i've made earlier and I want to plot both on the same figure what should I do?
Here is the code of my contourf plot:
import pylab as pl
from pylab import *
import xlrd
import math
import itertools
from matplotlib import collections as mc
import matplotlib.pyplot as plt
import copy as dc
import pyexcel
from pyexcel.ext import xlsx
import decimal
x_list = linspace(0, 99, 100)
y_list = linspace(0, 99, 100)
X, Y = meshgrid(x_list, y_list, indexing='xy')
Z = [[0 for x in range(len(x_list))] for x in range(len(y_list))]
for each_axes in range(len(Z)):
for each_point in range(len(Z[each_axes])):
Z[len(Z)-1-each_axes][each_point] = power_at_each_point(each_point, each_axes)
figure()
CP2 = contourf(X, Y, Z, cmap=plt.get_cmap('Reds'))
colorbar(CP2)
title('Coverage Plot')
xlabel('x (m)')
ylabel('y (m)')
show()
This is the code of my previously plotted plot:
lc = mc.LineCollection(lines, linewidths=3)
fig, ax = pl.subplots()
ax.add_collection(lc)
ax.autoscale()
ax.margins(0.05)
#The code blow is just for drawing the final plot of the building.
Nodes = xlrd.open_workbook(Node_file_location)
sheet = Nodes.sheet_by_index(0)
Node_Order_Counter = range(1, sheet.nrows + 1)
In_Node_Order_Counter = 0
for counter in range(len(Node_Positions_Ascending)):
plt.plot(Node_Positions_Ascending[counter][0], Node_Positions_Ascending[counter][1], marker='o', color='r',
markersize=6)
pl.text(Node_Positions_Ascending[counter][0], Node_Positions_Ascending[counter][1],
str(Node_Order_Counter[In_Node_Order_Counter]),
color="black", fontsize=15)
In_Node_Order_Counter += 1
#Plotting the different node positions on our plot & numbering them
pl.show()
Without your data we can't see what the plot is supposed to look like, but I have some general recommendations.
Don't use pylab. And if you absolutely must use it, use it within its namespace, and don't do from pylab import *. It makes for very sloppy code - for example, linspace and meshgrid are actually from numpy, but it's hard to tell that when you use pylab.
For complicated plotting, don't even use pyplot. Instead, use the direct object plotting interface. For example, to make a normal plot on top of a contour plot, (such as you want to do) you could do the following:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = np.linspace(1, 5, 20)
y = np.linspace(2, 5, 20)
z = x[:,np.newaxis] * (y[np.newaxis,:])**2
xx, yy = np.meshgrid(x, y)
ax.contourf(xx, yy, z, cmap='Reds')
ax.plot(x, 0.2*y**2)
plt.show()
Notice that I only used pyplot to create the figure and axes, and show them. The actual plotting is done using the AxesSubplot object.

How can I make legend error bars horizontal in matplotlib

I want to make a graph with matplotlib where the error bars in the graph are vertical,
but the error bar in legend is horizontal. The example code (below) produces a
graph where the error bar in the legend is vertical.
How can I make the legend error bar horizontal?
code:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 6)
y = np.sin(x)
dy = 0.1*np.abs(y)
plt.errorbar(x, y, yerr = dy, label="data", fmt='o')
plt.legend(loc="upperright", numpoints=1, frameon=False)
plt.show()
In the produced graph, I want the error bar inside the legend to be horizontal, while the error bars in the rest of the graph remain vertical. I want this so that the error bar in the legend is not confused for a data point. How can I accomplish this?
You can retrieve the error bar line object from the default legend and then create a custom legend with it and it will automatically be drawn horizontally, like this:
import numpy as np # v 1.19.2
import matplotlib.pyplot as plt # v 3.3.2
x = np.linspace(0, 2*np.pi, 6)
y = np.sin(x)
dy = 0.1*np.abs(y)
fig, ax = plt.subplots()
plt.errorbar(x, y, yerr=dy, label='data', fmt='o', ecolor='red')
# Retrieve handles and labels: note the tuple within the tuple to
# unpack the handles
(errorbar_container,), labels = ax.get_legend_handles_labels()
point, line = errorbar_container.get_children()
# Create the custom legend: note that the handles are drawn on top of
# one another in the order that they are listed in the tuple
plt.legend([(line, point)], labels, frameon=False)
plt.show()

Categories

Resources