How remove data points from text annotate? - python

The following script generates a scatter plot with annotated data points. I'd like remove circle markers from the plot and just show the labels.
fig, ax = Plot.subplots()
ax.scatter(Y0_mean, Y1_mean)
for i, txt in enumerate(features.playerCountry.unique()):
country_name = countries_code[countries_code.CountryCode == txt]
['ctr'].values[0].lower()
ax.annotate(country_name, (Y0_mean[i], Y1_mean[i]), xytext=(Y0_mean[i],
Y1_mean[i]), size=5)
ax.legend(fontsize=8)
fig.savefig(figPath + 'LocationAwareMeanFeatures_ctr'+str(lr), dpi=300)

There are 2 options. 1) don't call ax.scatter. This does mean you have to set the axes limits yourself in order to see the points.
y=[2.56422, 3.77284,3.52623,3.51468,3.02199]
x=[0.15, 0.3, 0.45, 0.6, 0.75]
n=[58,651,393,203,123]
fig, ax = plt.subplots()
# ax.scatter(x, y)
for i, txt in enumerate(n):
ax.annotate(txt, (x[i],y[i]))
ax.set_ylim(2.5,4)
plt.show()
or option 2) Call ax.scatter but remove the LineCollections that are added by doing:
y=[2.56422, 3.77284,3.52623,3.51468,3.02199]
x=[0.15, 0.3, 0.45, 0.6, 0.75]
n=[58,651,393,203,123]
fig, ax = plt.subplots()
points = ax.scatter(x, y)
for i, txt in enumerate(n):
ax.annotate(txt, (x[i],y[i]))
points.remove()
plt.show()
Both methods give the same result (provided you set the same axis limits in option 1 as you get in option 2):

Related

How to preserve axis aspect ratio with tight_layout

I have a plot with both a colorbar and a legend. I want to place the legend outside of the plot to the right of the colorbar. To accomplish this, I use bbox_to_anchor argument, but this causes the legend to get cut off:
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm
_, ax = plt.subplots()
extent = np.r_[0, 1, 0, 1]
space = np.linspace(0, 1)
probs = np.array([[norm.cdf(x + y) for x in space] for y in space])
colormap = ax.imshow(probs, aspect="auto", origin="lower", extent=extent, alpha=0.5)
colorbar = plt.colorbar(colormap, ax=ax)
colorbar.set_label(f"Probability")
ax.scatter(
[0.2, 0.4, 0.6], [0.8, 0.6, 0.4], color="r", label="Labeled Points",
)
plt.legend(loc="center left", bbox_to_anchor=(1.3, 0.5))
plt.title
plt.show()
Plot with legend cut off
To fix the legend, I insert a call to plt.tight_layout() before plt.show(), but this causes the aspect ratio to get distorted:
Plot with distorted aspect ratio
How can I show the entire legend and preserve the aspect ratio of the axes?
You can manage the ratio between axis height and width with matplotlib.axes.Axes.set_aspect. Since you want them to be equal:
ax.set_aspect(1)
Then you can use matplotlib.pyplot.tight_layout to fit the legend within the figure.
If you want to adjust margins too, you can use matplotlib.pyplot.subplots_adjust.
Complete Code
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm
_, ax = plt.subplots()
extent = np.r_[0, 1, 0, 1]
space = np.linspace(0, 1)
probs = np.array([[norm.cdf(x + y) for x in space] for y in space])
colormap = ax.imshow(probs, aspect="auto", origin="lower", extent=extent, alpha=0.5)
colorbar = plt.colorbar(colormap, ax=ax)
colorbar.set_label(f"Probability")
ax.scatter([0.2, 0.4, 0.6], [0.8, 0.6, 0.4], color="r", label="Labeled Points",)
plt.legend(loc="center left", bbox_to_anchor=(1.3, 0.5))
ax.set_aspect(1)
plt.tight_layout()
plt.subplots_adjust(left = 0.1)
plt.show()

matplotlib: different handletextpad values in the same legend

I have the following script:
fig = plt.figure()
fig.set_size_inches(8,7)
ax1 = fig.add_subplot(1,1,1)
### PLOT
for k in my_dict:
x, y = my_dict[k][0], my_dict[k][1]
ax1.plot(x, y, linewidth = 0, marker='o', markersize = 4)
X = np.logspace(0.3, 6.6)
ax1.plot(X, 2*X**(-2), linewidth = 2, c='k')
X = np.logspace(0.3, 7.7)
ax1.plot(X, 3*X**(-1.5), linewidth = 2, c='b')
ax1.set_xscale('log')
ax1.set_yscale('log')
## LEGEND
labels = ['$10^{-1} \, \\Delta_1^*$', '$\\Delta_1^*$',\
'$10^{5/2} \, \\Delta_1^*$', '$10^3 \, \\Delta_1^*$',
'$x^{-2}$', '$x^{-3/2}$']
curves = ax1.get_lines()
legend1 = ax1.legend([curves[0], curves[1], curves[2]],\
[labels[0], labels[1], labels[2]],\
loc=1, ncol=1, fancybox=False, shadow=False,\
framealpha=0.0, markerscale=2, fontsize=25, handletextpad=0.0)
legend2 = ax1.legend([curves[3], curves[4], curves[5]],\
[labels[3], labels[4], labels[5]],\
loc=3, ncol=1, fancybox=False, shadow=False,\
framealpha=0.0, markerscale=2, fontsize=25, handletextpad=0.0)
vp = legend1._legend_box._children[-1]._children[0]
for c in vp._children:
c._children.reverse()
vp.align="right"
ax1.add_artist(legend1)
ax1.add_artist(legend2)
fig.tight_layout()
plt.show()
The result is
The issue: I use handletextpad in the legends, and that is because I need points and text to be very close. However the last two elements in the second legend are not points, but lines. They take more space then points and the text happens to be too close.
I need to keep this distance between text and points while increasing the distance between text and lines in the same legend.
I tried with handletextpad=[0.1, 0.5, 0.5] and similar strategies, but I haven't been able to set individual values of handletextpad.
Another possibility would be to make separate legends and specifically one with only lines. This, however, would force me to manually positioning any legend very carefully and I'd rather not doing it. Also (I don't know if it can help), but I'd rather not replace
plt.plot(x, y, linewidth = 0, markersize = 4)
with
plt.scatter(x, y).
Except for these two caveats, everything is welcome.

Title, tick, axis labels, nothing is showing in matplotlib

This is my code:
import numpy as np
import matplotlib.pyplot as plt
def plot_graph():
fig = plt.figure()
data = [[top3_empsearch, top5_empsearch, top7_empsearch], [top3_elastic, top5_elastic, top7_elastic]]
X = np.arange(3)
ax = fig.add_axes([0, 0, 1, 1])
ax.bar(X + 0.00, data[0], color='b', width=0.25)
ax.bar(X + 0.25, data[1], color='g', width=0.25)
ax.set_ylabel('Accuracy (in %)')
plt.title('Percentage accuracy for selected result in Top-3, Top-5, Top-7 in employee search vs elastic search')
plt.yticks(np.arange(0, 101, 10))
colors = {'empsearch':'blue', 'elastic':'green'}
labels = list(colors.keys())
handles = [plt.Rectangle((0,0),1,1, color=colors[label]) for label in labels]
plt.legend(handles, labels)
plt.style.use('dark_background')
plt.show()
plot_graph()
The outcome of this code is ->
No ticks, no labels, no title nothing is visible and I'm bamboozled. Will appreciate the help.
The only problem is in this line:
ax = fig.add_axes([0, 0, 1, 1])
Looking to the bibliography (https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.figure.Figure.html), you will see that the first parameter of add_axes() function is "rect", which refers to the the dimensions [left, bottom, width, height] of the new axes, all quantitie in fractions of figure width and height. So in your code you are giving exactly the dimensions of the figure, so the title, ticks, labels... are there but hidden. So you have to leave some space, reducing a bit the plot's dimensions. You could do it just by modifying:
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
Alternatively, you could replace that line by:
ax = fig.add_subplot(1,1,1)
and the result should be the same.
Here is my result:

How to plot to subplots during a loop

How can I create one plot for this loop?
I want to create some subplots according to i values taken from the loop. Do I need to create a new For/Loop which goes to each subplot? How can I do it?. This is my code:
fig, axes = plt.subplots(nrows=4, ncols=3)
fig.subplots_adjust(hspace=0.5)
fig.suptitle('Main plots')
for i in range(1,13):
month = [i]
DF_sub = DF[DF['months'].isin(month)]
out = pd.cut(DF_sub['new'], bins=[0, 0.25, 0.5, 0.75, 1], include_lowest=True)
out_norm = out.value_counts(sort=False, normalize=True)
ax = out_norm.plot.bar(rot=0, color="b", figsize=(6,4))
plt.title('Subplot -' + str(i))
Up to now, I just get the last one, but I am missing the previos i-s values from the loop
You need to pass the ax parameter to plot.bar to specify on which of your axes, returned from plt.subplots, the bar chart should be plotted, i.e.:
fig, axes = plt.subplots(nrows=4, ncols=3)
fig.subplots_adjust(hspace=0.5)
fig.suptitle('Main plots')
for i in range(1,13):
month = [i]
ax = axes[i - 1]
DF_sub = DF[DF['months'].isin(month)]
out = pd.cut(DF_sub['new'], bins=[0, 0.25, 0.5, 0.75, 1], include_lowest=True)
out_norm = out.value_counts(sort=False, normalize=True)
out_norm.plot.bar(rot=0, color="b", figsize=(6,4), ax=ax)
plt.title('Subplot -' + str(i))
If you don't pass the ax parameter, the bar chart will automatically be plotted on the currently active axis, which is the one most recently created unless set otherwise.

Place transect through two subplot

I have created a plot that contains two plots within the same axis similar to this:
https://matplotlib.org/1.5.1/examples/pylab_examples/ganged_plots.html
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.5, 0.8, 0.4],
xticklabels=[], ylim=(-1.2, 1.2))
ax2 = fig.add_axes([0.1, 0.1, 0.8, 0.4],
ylim=(-1.2, 1.2))
x = np.linspace(0, 10)
ax1.plot(np.sin(x))
ax2.plot(np.cos(x));
Is there a way I can place transect lines that pass through both plots if I have the specific x coordinate for each line?
IIUC, you can do something like this using list comprehension on a list of your axes:
[i.axvline(20, color='r') for i in [ax1,ax2]]
Output:

Categories

Resources