python - repositioning my legend using loc - python

this is the output of my code
as you can see both legends 'pl' and 'ppl' are overlapping at the top right. How do I get one of them to move to top left.
I tried searching for ans, and used "loc" to fix the issue, somehow I continue getting error. Can someone help please?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.set_xlabel('Date')
ax1.set_ylabel('percent change / 100')
dd = pd.DataFrame(np.random.randint(1,10,(30,2)),columns=['pl','ppl'])
dd['pl'].plot(ax=ax1,legend=True)
dd['ppl'].plot(ax=ax2, style=['g--', 'b--', 'r--'],legend=True)
ax2.set_ylabel('difference')
plt.show()

Perhaps plot directly with matplotlib instead of using DataFrame.plot:
ax1.plot(dd['pl'], label='pl')
ax1.legend(loc='upper left')
ax2.plot(dd['ppl'], ls='--', c='g', label='ppl')
ax2.legend(loc='upper right')
Output:

I think you need to call legend on plot and position the legend accordingly. Please see below.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.set_xlabel('Date')
ax1.set_ylabel('percent change / 100')
dd = pd.DataFrame(np.random.randint(1,10,(30,2)),columns=['pl','ppl'])
dd['pl'].plot(ax=ax1, legend=True).legend(loc='center left',bbox_to_anchor=(1.0, 0.5))
dd['ppl'].plot(ax=ax2, style=['g--', 'b--', 'r--'],legend=True).legend(loc='upper right')

You can create the legend in several ways:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.set_xlabel("Date")
ax1.set_ylabel("percent change / 100")
dd = pd.DataFrame(np.random.randint(1, 10, (30, 2)), columns=["pl", "ppl"])
dd["pl"].plot(ax=ax1)
dd["ppl"].plot(ax=ax2, style=["g--", "b--", "r--"])
# # two separate legends
# ax1.legend()
# ax2.legend(loc="upper left")
# # a single legend for the whole fig
# fig.legend(loc="upper right")
# # a single legend for the axis
# get the lines in the axis
lines1 = ax1.lines
lines2 = ax2.lines
all_lines = lines1 + lines2
# get the label for each line
all_labels = [lin.get_label() for lin in all_lines]
# place the legend
ax1.legend(all_lines, all_labels, loc="upper left")
ax2.set_ylabel("difference")
plt.show()
The last one I left uncommented creates a single legend inside the ax, with both lines listed.
Cheers!

Related

Markers in beginning and end of line plots

I have 5 datasets that have thousands of x and y coordinates grouped by 'frame' that create 5 trajectory plots. I'd like to mark the first and last coordinates for each plot but having difficulty figuring it out. I am using Jupiter Notebook.
mean_pos1 = gr1.mean()
mean_pos2 = gr2.mean()
mean_pos3 = gr3.mean()
mean_pos4 = gr4.mean()
mean_pos5 = gr5.mean()
plt.figure()
xlim=(200, 1500)
ylim=(0, 1200)
ax1 = mean_pos1.plot(x='x', y='y',color='blue',label='Dolphin A'); ax1.set_title('mean trajectory');
ax2 = mean_pos2.plot(x='x', y='y',color='red',label='Dolphin B'); ax2.set_title('mean trajectory');
ax3 = mean_pos3.plot(x='x', y='y',color='green',label='Dolphin C'); ax3.set_title('mean trajectory');
ax4 = mean_pos4.plot(x='x', y='y',color='magenta',label='Dolphin D'); ax4.set_title('mean trajectory');
ax5 = mean_pos5.plot(x='x', y='y',color='cyan',label='Dolphin E'); ax5.set_title('mean trajectory');
ax1.set_xlim(xlim)
ax1.set_ylim(ylim)
ax2.set_xlim(xlim)
ax2.set_ylim(ylim)
ax3.set_xlim(xlim)
ax3.set_ylim(ylim)
ax4.set_xlim(xlim)
ax4.set_ylim(ylim)
ax5.set_xlim(xlim)
ax5.set_ylim(ylim)
plt.show()
the output of them looks like this:
Use the scatter method to plot the markers separately on the same axis by grabbing the first and last elements from your x and y series:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({'x': np.random.normal(3,0.2,10), 'y': np.random.normal(5,0.3,10)})
fig, ax = plt.subplots()
df.plot(x='x', y='y', ax=ax)
ax.scatter(df['x'].iloc[0], df['y'].iloc[0], marker='o', color='red')
ax.scatter(df['x'].iloc[-1], df['y'].iloc[-1], marker='o', color='red')
plt.show()

How to reduce horizontal spacing between subplots in matplotlib python? [duplicate]

The code below produces gaps between the subplots. How do I remove the gaps between the subplots and make the image a tight grid?
import matplotlib.pyplot as plt
for i in range(16):
i = i + 1
ax1 = plt.subplot(4, 4, i)
plt.axis('on')
ax1.set_xticklabels([])
ax1.set_yticklabels([])
ax1.set_aspect('equal')
plt.subplots_adjust(wspace=None, hspace=None)
plt.show()
The problem is the use of aspect='equal', which prevents the subplots from stretching to an arbitrary aspect ratio and filling up all the empty space.
Normally, this would work:
import matplotlib.pyplot as plt
ax = [plt.subplot(2,2,i+1) for i in range(4)]
for a in ax:
a.set_xticklabels([])
a.set_yticklabels([])
plt.subplots_adjust(wspace=0, hspace=0)
The result is this:
However, with aspect='equal', as in the following code:
import matplotlib.pyplot as plt
ax = [plt.subplot(2,2,i+1) for i in range(4)]
for a in ax:
a.set_xticklabels([])
a.set_yticklabels([])
a.set_aspect('equal')
plt.subplots_adjust(wspace=0, hspace=0)
This is what we get:
The difference in this second case is that you've forced the x- and y-axes to have the same number of units/pixel. Since the axes go from 0 to 1 by default (i.e., before you plot anything), using aspect='equal' forces each axis to be a square. Since the figure is not a square, pyplot adds in extra spacing between the axes horizontally.
To get around this problem, you can set your figure to have the correct aspect ratio. We're going to use the object-oriented pyplot interface here, which I consider to be superior in general:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8,8)) # Notice the equal aspect ratio
ax = [fig.add_subplot(2,2,i+1) for i in range(4)]
for a in ax:
a.set_xticklabels([])
a.set_yticklabels([])
a.set_aspect('equal')
fig.subplots_adjust(wspace=0, hspace=0)
Here's the result:
You can use gridspec to control the spacing between axes. There's more information here.
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
plt.figure(figsize = (4,4))
gs1 = gridspec.GridSpec(4, 4)
gs1.update(wspace=0.025, hspace=0.05) # set the spacing between axes.
for i in range(16):
# i = i + 1 # grid spec indexes from 0
ax1 = plt.subplot(gs1[i])
plt.axis('on')
ax1.set_xticklabels([])
ax1.set_yticklabels([])
ax1.set_aspect('equal')
plt.show()
Without resorting gridspec entirely, the following might also be used to remove the gaps by setting wspace and hspace to zero:
import matplotlib.pyplot as plt
plt.clf()
f, axarr = plt.subplots(4, 4, gridspec_kw = {'wspace':0, 'hspace':0})
for i, ax in enumerate(f.axes):
ax.grid('on', linestyle='--')
ax.set_xticklabels([])
ax.set_yticklabels([])
plt.show()
plt.close()
Resulting in:
With recent matplotlib versions you might want to try Constrained Layout. This does (or at least did) not work with plt.subplot() however, so you need to use plt.subplots() instead:
fig, axs = plt.subplots(4, 4, constrained_layout=True)
Have you tried plt.tight_layout()?
with plt.tight_layout()
without it:
Or: something like this (use add_axes)
left=[0.1,0.3,0.5,0.7]
width=[0.2,0.2, 0.2, 0.2]
rectLS=[]
for x in left:
for y in left:
rectLS.append([x, y, 0.2, 0.2])
axLS=[]
fig=plt.figure()
axLS.append(fig.add_axes(rectLS[0]))
for i in [1,2,3]:
axLS.append(fig.add_axes(rectLS[i],sharey=axLS[-1]))
axLS.append(fig.add_axes(rectLS[4]))
for i in [1,2,3]:
axLS.append(fig.add_axes(rectLS[i+4],sharex=axLS[i],sharey=axLS[-1]))
axLS.append(fig.add_axes(rectLS[8]))
for i in [5,6,7]:
axLS.append(fig.add_axes(rectLS[i+4],sharex=axLS[i],sharey=axLS[-1]))
axLS.append(fig.add_axes(rectLS[12]))
for i in [9,10,11]:
axLS.append(fig.add_axes(rectLS[i+4],sharex=axLS[i],sharey=axLS[-1]))
If you don't need to share axes, then simply axLS=map(fig.add_axes, rectLS)
Another method is to use the pad keyword from plt.subplots_adjust(), which also accepts negative values:
import matplotlib.pyplot as plt
ax = [plt.subplot(2,2,i+1) for i in range(4)]
for a in ax:
a.set_xticklabels([])
a.set_yticklabels([])
plt.subplots_adjust(pad=-5.0)
Additionally, to remove the white at the outer fringe of all subplots (i.e. the canvas), always save with plt.savefig(fname, bbox_inches="tight").

How to delete legend in pandas

I try to plot with both pandas (pd) and matplotlib.pyplot (plt). But I don't want pandas to show legend yet I still need the plt legend. Is there a way I could delete the legend of pandas plot? (legend=False doesn't work)
import pandas as pd
import matplotlib.pyplot as plt
xs = [i for i in range(1, 11)]
ys = [i for i in range(1, 11)]
df = pd.DataFrame(list(zip(xs, ys)), columns=['xs', 'ys'])
fig, ax = plt.subplots()
# plot pd data-frame, I don't want this to show legend
df.plot(x='xs', y='ys', ax=ax, kind='line', legend=False)
# these doesn't work
ax.legend([])
ax.get_legend().remove()
ax.legend().set_visible(False)
# plot by plt, I only want this to show legend
ax.plot(xs, ys, label='I only need this label to be shown')
ax.legend()
plt.show() # still showing both legends
Note: I prefer not to change the order of plotting (even though plot plt first and then pd could allow showing only plt legend, but the plt plot will get block by pd plot), and not using plt to plot the dataframe's data
You can remove the 1st set of lines and labels from the legend:
fig, ax = plt.subplots()
df.plot(x='xs', y='ys', ax=ax, kind='line', label='Something')
ax.plot(xs, ys, label='I only need this label to be shown')
# Legend except 1st lines/labels
lines, labels = ax.get_legend_handles_labels()
ax.legend(lines[1:], labels[1:])
plt.show()
You can use matplotlib to plot DataFrame data (and other data from other sources) on the same plot without using df.plot(). Do you need to use df.plot(), or would this be okay?
import pandas as pd
import matplotlib.pyplot as plt
xs = [i for i in range(1, 11)]
ys = [i for i in range(1, 11)]
df = pd.DataFrame(list(zip(xs, ys)), columns=['xs', 'ys'])
fig, ax = plt.subplots()
#just keep using mpl but reference the data in the dataframe, basically what df.plot() does
ax.plot(df['xs'], df['ys'])
ax.plot(xs, ys, label='I only need this label to be shown')
ax.legend()
plt.show()
If you do insist on using df.plot(), you can still take advantage of the underscore trick, as described in the documentation:
Specific lines can be excluded from the automatic legend element selection by defining a label starting with an underscore.
import pandas as pd
import matplotlib.pyplot as plt
xs = [i for i in range(1, 11)]
ys = [i for i in range(1, 11)]
df = pd.DataFrame(list(zip(xs, ys)), columns=['xs', 'ys'])
fig, ax = plt.subplots()
# plot pd data-frame, I don't want this to show legend
df.plot(x='xs', y='ys', ax=ax, kind='line', label='_hidden')
# plot by plt, I only want this to show legend
ax.plot(xs, ys, label='I only need this label to be shown')
ax.legend()
plt.show() # still showing both legends
This will yield the same result as above, but I get a warning (UserWarning: The handle <matplotlib.lines.Line2D object at 0x00000283F0FFDB38> has a label of '_hidden' which cannot be automatically added to the legend.). This feels messier and more hacky, so I prefer the first option.
Use label='_nolegend_' as recommended here. This worked for me:
import pandas as pd
import matplotlib.pyplot as plt
xs = [i for i in range(1, 11)]
ys = [i for i in range(1, 11)]
df = pd.DataFrame(list(zip(xs, ys)), columns=['xs', 'ys'])
fig, ax = plt.subplots()
# plot pd data-frame, I don't want this to show legend
df.plot(x='xs', y='ys', ax=ax, kind='line', label='_nolegend_')
# plot by plt, I only want this to show legend
ax.plot(xs, ys, label='I only need this label to be shown')
ax.legend()
plt.show() # now showing one legend

Only one dataname shows in the legend

I like to display a diagram of two data columns. The problem about it is that the legend shows only the last name l/s.
Here is my diagram:
import pandas as pd
import matplotlib.pyplot as plt
Tab = pd.read_csv('Mst01.csv', delimiter=';')
x = Tab['Nr. ']
y1 = Tab['cm']
y2 = Tab['l/s']
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(x, y1, 'g-', label='cm')
ax2.plot(x, y2, 'b-', label='l/s')
ax1.set_xlabel('Nr.')
ax1.set_ylabel('cm', color='g')
ax2.set_ylabel('l/s', color='b')
plt.title('Mst01')
plt.legend()
plt.show()
If I do
ax1.legend()
ax2.legend()
both legends will displayed but one above the other.
By the way, is there a easyier way the get the spaces for every line of code?
Good evening!
so you got two possibilities either you add the plots together or you use fig.legend()
here is some sample code which yields to the solution
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Create Example Dataframe
dummy_data = np.random.random_sample((100,2))
df = pd.DataFrame(dummy_data, columns=['Col_1', 'Col_2'])
df.Col_2 = df.Col_2*100
# Create Figure
fig, ax = plt.subplots()
col_1 = ax.plot(df.Col_1, label='Col_1', color='green')
ax_2 = ax.twinx()
col_2 = ax_2.plot(df.Col_2, label='Col_2', color='r')
# first solution
lns = col_1+col_2
labs = [l.get_label() for l in lns]
ax.legend(lns, labs, loc='upper right')
# secound solution
fig.legend()
fig
The solution can be derived from this question.
What do you mean by spaces? you mean the indention of e.g. a for loop?

remove overlapping tick marks on subplot in matplotlib

I've create the following set of subplots using the following function:
def create31fig(size,xlabel,ylabel,title=None):
fig = plt.figure(figsize=(size,size))
ax1 = fig.add_subplot(311)
ax2 = fig.add_subplot(312)
ax3 = fig.add_subplot(313)
plt.subplots_adjust(hspace=0.001)
plt.subplots_adjust(wspace=0.001)
ax1.set_xticklabels([])
ax2.set_xticklabels([])
xticklabels = ax1.get_xticklabels()+ ax2.get_xticklabels()
plt.setp(xticklabels, visible=False)
ax1.set_title(title)
ax2.set_ylabel(ylabel)
ax3.set_xlabel(xlabel)
return ax1,ax2,ax3
How do I make sure the top and bottom of subplot(312) do not overlap with their neighbours? Thanks.
In the ticker module there is a class called MaxNLocator that can take a prune kwarg.
Using that you can remove the topmost tick of the 2nd and 3rd subplots:
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator # added
def create31fig(size,xlabel,ylabel,title=None):
fig = plt.figure(figsize=(size,size))
ax1 = fig.add_subplot(311)
ax2 = fig.add_subplot(312)
ax3 = fig.add_subplot(313)
plt.subplots_adjust(hspace=0.001)
plt.subplots_adjust(wspace=0.001)
ax1.set_xticklabels([])
ax2.set_xticklabels([])
xticklabels = ax1.get_xticklabels() + ax2.get_xticklabels()
plt.setp(xticklabels, visible=False)
ax1.set_title(title)
nbins = len(ax1.get_xticklabels()) # added
ax2.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) # added
ax2.set_ylabel(ylabel)
ax3.yaxis.set_major_locator(MaxNLocator(nbins=nbins,prune='upper')) # added
ax3.set_xlabel(xlabel)
return ax1,ax2,ax3
create31fig(5,'xlabel','ylabel',title='test')
Sample image after making those adjustments:
Aside: If the overlapping x- and y- labels in the lowest subplot are an issue consider "pruning" one of those as well.

Categories

Resources