Prevent axes from cutting off dots in matplotlib scatter plots - python

import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [0, 1, 7, 2]
plt.scatter(x, y, color='red')
plt.title('number of iterations')
plt.xlim([1, 4])
plt.ylim([1, 8])
If one was to plot this data, the dots on the axes are partially cut off. Is there a way to prevent this (i.e. can the dots be plotted on top of the axes)?

Setting the clip_on attribute to False allows you to go beyond the axes, but by default the axes will be on top. For example, the script
x = [1, 2, 3, 4]
y = [0, 1, 7, 2]
plt.scatter(x, y, color="red", clip_on=False)
plt.title('number of iterations')
plt.xlim([1, 4])
plt.ylim([1, 8])
Yields the following.
Note that the axes "cut through" the dots. If you want the dots to go on top of the axes/labels, you need to change the default zorder. For example, the script
x = [1, 2, 3, 4]
y = [0, 1, 7, 2]
plt.scatter(x, y, color="red", clip_on=False, zorder = 10)
plt.title('number of iterations')
plt.xlim([1, 4])
plt.ylim([1, 8])
yields
Note: any zorder value 3 or greater will work here.

Set clip_on to False:
plt.scatter(x, y, color='red', clip_on=False)

Related

Determine plot size with grid Matplotlib

I'm plotting some data using plt.scatter(), and I want to change the size of the plot which is it on, however the only results which come up when you search 'change plot size' are things to do with changing the figure's size, which I am not looking to do.
To visualise my issue, I have a reproducible example where I'm trying to plot 4 points on a 10x10 grid, however the size of the scatter plot is determined by the data not the grid
The two graphs above demonstrate my problem, I am trying to plot the four points on the left graph on the 10x10 grid seen on the right graph. I have added in a datapoint at (10, 10) to show this.
My code is currently:
x = [1, 2, 3, 4]
y = [1, 2, 3, 4]
fig = plt.figure()
ax = fig.gca()
ax.set_xticks(np.arange(0, 11, 1))
ax.set_yticks(np.arange(0, 11, 1))
plt.grid()
plt.scatter(x, y)
Which produces the left graph.
IIUC:
x = [1, 2, 3, 4]
y = [1, 2, 3, 4]
fig = plt.figure()
plt.xlim(0, 10)
plt.ylim(0, 10)
plt.grid()
plt.scatter(x, y)
Output:
Just change the limits of x and y axes:
plt.xlim(0,11)
plt.ylim(0,11)

Upper limit with upper error

Is it possibile to have an upper limit (with the down arrow) with the point centered in the the best value and, at the same time, the upper error?
Something like this:
I'm trying with:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([10, 15, 20, 25, 30, 35])
x_el = np.array([1, 1, 2, 25, 1, 2, 1])
x_eu = np.array([1, 1, 2, 1, 1, 2, 1])
y = np.array([29, 15, 9, 10, 25, 14])
y_el = np.array([1, 1, 2, 1, 1, 2, 1])
y_eu = np.array([11,1,2,1,1,2,1])
fig, ax = plt.subplots()
for i in range(len(x)):
if (x[i] - x_el[i]) == 0:
el = 0
ax.errorbar(x[i], y[i], yerr=[[y_el[i]], [y_eu[i]]], xerr=[[el],[x_eu[i]]],
c='b', capsize=2, elinewidth=1, marker='o',
xuplims=True)
else:
ax.errorbar(x[i], y[i], yerr=[[y_el[i]], [y_eu[i]]], xerr=[[x_el[i]], [x_eu[i]]],
c='b', capsize=2, elinewidth=1, marker='o')
But this is the result:
The point number 4 has neither the uplim nor the upper error.
The short answer is yes, but you have to plot the upper limits and the error bars separately. Let's start by plotting your normal error bars properly. You can do this without looping if your data is in a numpy array already:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([10, 15, 20, 25, 30, 35])
x_el = np.array([1, 1, 2, 25, 1, 2])
x_eu = np.array([1, 1, 2, 1, 1, 2])
y = np.array([29, 15, 9, 10, 25, 14])
y_el = np.array([1, 1, 2, 1, 1, 2])
y_eu = np.array([11, 1, 2, 1, 1, 2])
fig, ax = plt.subplots()
mask = (x != x_el)
ax.errorbar(x, y, yerr=[y_el, y_eu], xerr=[x_el * mask, x_eu],
c='b', capsize=2, elinewidth=1, marker='o', linestyle='none')
Notice that I trimmed the error bar arrays down to the same size as x, which allows me to compute the mask using the != operator. Since you are interested in having all the error bars besides the one in x_el, I multiply by the mask. The mask is a boolean, and any error bar that is masked out will just be set to zero that way. All the other bars get plotted properly at this point:
Now you can use the same mask (but inverted) to plot the upper limits:
ax.errorbar(x[~mask], y[~mask], xerr=x_el[~mask],
c='b', capsize=2, elinewidth=1, marker='o', linestyle='none',
xuplims=True)
The result is
If you were not interested in having an obscenely long arrow that stretches to zero, you can shorten it to whatever size you like:
ax.errorbar(x[~mask], y[~mask], xerr=1,
c='b', capsize=2, elinewidth=1, marker='o', linestyle='none',
xuplims=True)
Alternative
You could even get pretty close with a single plotting call, since xuplims accepts an array of booleans. However, anywhere it is True will eliminate the right bar:
mask = (x == x_el)
ax.errorbar(x, y, yerr=[y_el, y_eu], xerr=[x_el, x_eu],
c='b', capsize=2, elinewidth=1, marker='o', linestyle='none',
xuplims=mask)
You end up having to fill in the right bars in this case:
ax.errorbar(x[mask], y[mask], xerr=[np.zeros_like(x_eu)[mask], x_eu[mask]],
c='b', capsize=2, elinewidth=1, marker='o', linestyle='none')

Matplotlib pyplot: Is it possible to have exponential axis?

i need "the opposite" of loglog for my pyplot chart.
How can i achive exponential axis?
Data looks like:
x = [1, 2, 4]
y = [1, 2, 4]
z = [2, 2, 2]
quadranten = plt.figure()
s = [20*4**n for n in z]
fig, ax = plt.subplots()
ax.axis([1, 5, 1, 5])
ax.loglog() <-- opposite function?
xstart, xend = ax.get_xlim()
ax.xaxis.set_ticks(np.arange(xstart, xend, 0.712123))
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%0.1f'))
ystart, yend = ax.get_ylim()
ax.yaxis.set_ticks(np.arange(ystart, yend, 0.712123))
ax.yaxis.set_major_formatter(ticker.FormatStrFormatter('%0.1f'))
plt.xlabel('x')
plt.ylabel('y')
plt.scatter(x,y,s=s)
plt.show()
The goal is the x axis to have evenly sized steps: 0, 1, 2, 4, 8,...
Same for the y axis with evenly sized steps getting exponentially bigger by a factor (for example two): 0, 1, 2, 4, 8, ...
Is this possible?
Something like this:
loglog takes arguments basex and basey that control the base of the logarithms:
loglog(arange(1,100), arange(1,100)**2, basex=2, basey=2)

Suggestions to plot overlapping lines in matplotlib?

Does anybody have a suggestion on what's the best way to present overlapping lines on a plot? I have a lot of them, and I had the idea of having full lines of different colors where they don't overlap, and having dashed lines where they do overlap so that all colors are visible and overlapping colors are seen.
But still, how do I that.
I have the same issue on a plot with a high degree of discretization.
Here the starting situation:
import matplotlib.pyplot as plt
grid=[x for x in range(10)]
graphs=[
[1,1,1,4,4,4,3,5,6,0],
[1,1,1,5,5,5,3,5,6,0],
[1,1,1,0,0,3,3,2,4,0],
[1,2,4,4,3,2,3,2,4,0],
[1,2,3,3,4,4,3,2,6,0],
[1,1,3,3,0,3,3,5,4,3],
]
for gg,graph in enumerate(graphs):
plt.plot(grid,graph,label='g'+str(gg))
plt.legend(loc=3,bbox_to_anchor=(1,0))
plt.show()
No one can say where the green and blue lines run exactly
and my "solution"
import matplotlib.pyplot as plt
grid=[x for x in range(10)]
graphs=[
[1,1,1,4,4,4,3,5,6,0],
[1,1,1,5,5,5,3,5,6,0],
[1,1,1,0,0,3,3,2,4,0],
[1,2,4,4,3,2,3,2,4,0],
[1,2,3,3,4,4,3,2,6,0],
[1,1,3,3,0,3,3,5,4,3],
]
for gg,graph in enumerate(graphs):
lw=10-8*gg/len(graphs)
ls=['-','--','-.',':'][gg%4]
plt.plot(grid,graph,label='g'+str(gg), linestyle=ls, linewidth=lw)
plt.legend(loc=3,bbox_to_anchor=(1,0))
plt.show()
I am grateful for suggestions on improvement!
Just decrease the opacity of the lines so that they are see-through. You can achieve that using the alpha variable. Example:
plt.plot(x, y, alpha=0.7)
Where alpha ranging from 0-1, with 0 being invisible.
imagine your panda data frame is called respone_times, then you can use alpha to set different opacity for your graphs. Check the picture before and after using alpha.
plt.figure(figsize=(15, 7))
plt.plot(respone_times,alpha=0.5)
plt.title('a sample title')
plt.grid(True)
plt.show()
Depending on your data and use case, it might be OK to add a bit of random jitter to artificially separate the lines.
from numpy.random import default_rng
import pandas as pd
rng = default_rng()
def jitter_df(df: pd.DataFrame, std_ratio: float) -> pd.DataFrame:
"""
Add jitter to a DataFrame.
Adds normal distributed jitter with mean 0 to each of the
DataFrame's columns. The jitter's std is the column's std times
`std_ratio`.
Returns the jittered DataFrame.
"""
std = df.std().values * std_ratio
jitter = pd.DataFrame(
std * rng.standard_normal(df.shape),
index=df.index,
columns=df.columns,
)
return df + jitter
Here's a plot of the original data from Markus Dutschke's example:
And here's the jittered version, with std_ratio set to 0.1:
Replacing solid lines by dots or dashes works too
g = sns.FacetGrid(data, col='config', row='outputs', sharex=False)
g.map_dataframe(sns.lineplot, x='lag',y='correlation',hue='card', linestyle='dotted')
Instead of random jitter, the lines can be offset just a little bit, creating a layered appearance:
import matplotlib.pyplot as plt
from matplotlib.transforms import offset_copy
grid = list(range(10))
graphs = [[1, 1, 1, 4, 4, 4, 3, 5, 6, 0],
[1, 1, 1, 5, 5, 5, 3, 5, 6, 0],
[1, 1, 1, 0, 0, 3, 3, 2, 4, 0],
[1, 2, 4, 4, 3, 2, 3, 2, 4, 0],
[1, 2, 3, 3, 4, 4, 3, 2, 6, 0],
[1, 1, 3, 3, 0, 3, 3, 5, 4, 3]]
fig, ax = plt.subplots()
lw = 1
for gg, graph in enumerate(graphs):
trans_offset = offset_copy(ax.transData, fig=fig, x=lw * gg, y=lw * gg, units='dots')
ax.plot(grid, graph, lw=lw, transform=trans_offset, label='g' + str(gg))
ax.legend(loc='upper left', bbox_to_anchor=(1.01, 1.01))
# manually set the axes limits, because the transform doesn't set them automatically
ax.set_xlim(grid[0] - .5, grid[-1] + .5)
ax.set_ylim(min([min(g) for g in graphs]) - .5, max([max(g) for g in graphs]) + .5)
plt.tight_layout()
plt.show()

looking for marker text option for pyplot.plot()

I am looking for a way to insert numbers or text into markers. There is nothing in the matplotlib.pyplot.plot(*args, **kwargs) documentation about that.
The default zoom level places markers on the edge, hence reducing the available space on which to inscribe text.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4 ,5]
y = [1, 4, 9, 6, 10]
plt.plot(x, y, 'ro',markersize=23)
plt.show()
As jkalden suggested, annotate would solve your problem. The function's xy-argument let you position the text so that you can place it on the marker's position.
About your "zoom" problem, matplotlib will by default stretch the frame between the smallest and largest values you are plotting. It results in your outer markers having their centers on the very edge of the figure, and only half of the markers are visible. To override the default x- and y-limits you can use set_xlim and set_ylim. Here an offset is define to let you control the marginal space.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4 ,5]
y = [1, 4, 9, 6, 10]
fig, ax = plt.subplots()
# instanciate a figure and ax object
# annotate is a method that belongs to axes
ax.plot(x, y, 'ro',markersize=23)
## controls the extent of the plot.
offset = 1.0
ax.set_xlim(min(x)-offset, max(x)+ offset)
ax.set_ylim(min(y)-offset, max(y)+ offset)
# loop through each x,y pair
for i,j in zip(x,y):
corr = -0.05 # adds a little correction to put annotation in marker's centrum
ax.annotate(str(j), xy=(i + corr, j + corr))
plt.show()
Here is how it looks:
This is a revision of #snake_charmer 's method. I used alignment options (instead of manual offsets) to center the text on the dot, and other options for color, size and boldness (weight) of the text.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4 ,5]
y = [1, 4, 9, 6, 10]
fig, ax = plt.subplots()
# instantiate a figure and ax object
# annotate is a method that belongs to axes
ax.plot(x, y, 'ro',markersize=23)
## controls the extent of the plot.
offset = 1.0
ax.set_xlim(min(x)-offset, max(x)+ offset)
ax.set_ylim(min(y)-offset, max(y)+ offset)
# loop through each x,y pair
for i,j in zip(x,y):
ax.annotate(str(j), xy=(i, j), color='white',
fontsize="large", weight='heavy',
horizontalalignment='center',
verticalalignment='center')
plt.show()
You can do this using MathText.
Here are the instructions from matplotlib.org
fig, ax = plt.subplots()
fig.subplots_adjust(left=0.4)
marker_style.update(mec="None", markersize=15)
markers = ["$1$", r"$\frac{1}{2}$", "$f$", "$\u266B$", r"$\mathcal{A}$"]
for y, marker in enumerate(markers):
# Escape dollars so that the text is written "as is", not as mathtext.
ax.text(-0.5, y, repr(marker).replace("$", r"\$"), **text_style)
ax.plot(y * points, marker=marker, **marker_style)
format_axes(ax)
fig.suptitle('mathtext markers', fontsize=14)
plt.show()

Categories