Why aren't both subplots showing tick marks in matplotlib? - python

So I'm trying to plot some data via a scatter graph, and I would expect matplotlib to automatically add tick marks, which it does do on the first subplot. However, the second subplot does not have ticks with labels added automatically. I have tried setting the ticks myself explicitly which didn't work (using xticks and xticklabels).
Below is my code:
axs[0].scatter(dates, yvar, color="black", label="Data")
axs[0].plot(dates, predictions, color="blue", label="Predicted Value")
axs[1].scatter(predictions, yvar, color="red", label="Prediction vs Data")
plt.xticks(())
plt.yticks(())
fig.legend()
plt.show()

This code, trying to replicate your problem, behaves as expected. It produces the matplotlib.pyplot.xticks:
Try to call your legend with each ax individually
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 360, 1)
y = np.sin(2 * x * np.pi / 180)
z = np.cos(2 * x * np.pi / 180)
fig, ax = plt.subplots(2)
ax[0].scatter(x, y, label='label1')
ax[0].plot(x, z, label='label2')
ax[1].scatter(y, z, label='label3')
ax[0].legend()
ax[1].legend()
plt.tight_layout()
plt.show()

Related

Matplotlib: Scatter plot in a loop over set of arrays with consistent scatter point size and color bar

I am trying to generate a scatter plot using dataframe series x & y and the size of the scatter data point using dataframe series z.
I should mention that I iterate through a set of each x,y, and z arrays and add the color plot outside the loop.
I see that the scatter sizes and color bar are generated at each iteration therefore scatter sizes are not consistent with all data points in the plot and also with the colorbar at the end. How do I solve this?
fig, ax = plt.subplots()
for x, y, z in arrays_of_xyz:
splot = ax.scatter(x.to_numpy(), y.to_numpy(), marker= 'o', s = z.to_numpy(), cmap ='viridis_r', c = z.to_numpy())
fig.tight_layout()
plt.colorbar(splot)
plt.show()
Gautham
Can't see in which way the sizes in the plot are inconsistent.
The colorbar can be inconsistent if you do not enforce consistent vmin and vmax when calling scatter.
Can you please try with the following code and tell more about inconsistencies you got:
import numpy as np
import matplotlib.pyplot as plt
num_sets = 3
colors = ("red", "green", "blue")
num_pts_per_set = 20
xs = np.random.randn(num_sets, num_pts_per_set)
ys = np.random.randn(num_sets, num_pts_per_set)
zs = (
np.random.rand(num_sets, num_pts_per_set)
* np.arange(1, num_sets + 1).reshape(-1, 1)
* 30
)
zmin = zs.min()
zmax = zs.max()
fig, (ax1, ax2) = plt.subplots(ncols=2)
ax1.set_title("Sizes according to z\nColors according to set #")
for i, (x, y, z, clr) in enumerate(zip(xs, ys, zs, colors)):
ax1.scatter(x, y, marker="o", s=z, c=clr, label=f"Set #{i}")
ax1.legend()
ax2.set_title("Facecolors according to z\nSizes according to set #")
for i, (x, y, z, clr) in enumerate(zip(xs, ys, zs, colors)):
splot = ax2.scatter(x, y, marker="o", c=z, edgecolors=clr, s=(i+1)*30, vmin=zmin, vmax=zmax, label=f"Set #{i}")
ax2.legend()
fig.colorbar(splot)
plt.show()

Hide radial tick labels matplotlib

I'm trying to plot a polar plot with this code:
import numpy as np
import matplotlib.pylab as plt
def power(angle, l, lam):
return 1/(lam) * ((np.cos(np.pi*l*np.cos(angle)/lam) - np.cos(np.pi*l/lam))/np.sin(angle))**2
fig = plt.figure(1)
ax = fig.add_subplot(111, projection='polar')
theta = np.linspace(0.001, 2*np.pi, 100)
P1 = power(theta, 1, 5)
ax.plot(theta, P1, color='r', linewidth=3)
plt.savefig('1.png')
and I get this plot:
I would like to change 2 things. The first and more important one is to hide the radial tick labels (I just want to show the general form of the plot).
If possible, how can I choose the vertical axis to correspond to 0°?
Thanks for your help.
You can use set_yticklabels() to remove the radial ticks and set_theta_zero_location() to change the zero location:
fig = plt.figure(1)
ax = fig.add_subplot(111, projection='polar')
ax.plot(theta, P1, color='r', linewidth=3)
ax.set_yticklabels([])
ax.set_theta_zero_location('N')
plt.show()
You might also want to change the direction of the azimuthal axis:
ax.set_theta_direction(-1)
You can set the theta zero position with ax.set_theta_zero_location('N').
To modify the r tick labels, you could do something like
for r_label in ax.get_yticklabels():
r_label.set_text('')
If you want to remove them entirely, do ax.set_yticklabels([]).
More methods can be found in the PolarAxes documentation.

Residual plot not aligned with main graph

What is wrong with my residual plot that is causing to not be aligned with my main graph? My code is below.
import matplotlib.pyplot as plt
from scipy import stats
import numpy as np
x = np.array([0.030956,0.032956,0.034956,0.036956,0.038956,0.040956])
y = np.array([10.57821088,11.90701212,12.55570876,13.97542486,16.05403248,16.36634177])
yerr = [0.101614114,0.363255259,0.057234211,0.09289917,0.093288198,0.420165796]
xerr = [0.00021]*len(x)
fig1 = plt.figure(1)
frame1=fig1.add_axes((.1,.3,.8,.6))
m, b = np.polyfit(x, y, 1)
print 'gradient',m,'intercept',b
plt.plot(x, m*x + b, '-', color='grey', alpha=0.5)
plt.plot(x,y,'.',color='black',markersize=6)
plt.errorbar(x,y,xerr=0,yerr=yerr,linestyle="None",color='black')
plt.ylabel('$1/\sqrt{F}$ $(N)$',fontsize=20)
plt.autoscale(enable=True, axis=u'both', tight=True)
plt.grid(False)
frame2=fig1.add_axes((.1,.1,.8,.2))
s = m*x+b #(np.sqrt(4*np.pi*8.85E-12)/2.23E-8)*x
difference = y-s
plt.plot(x, difference, 'ro')
frame2.set_ylabel('$Residual$',fontsize=20)
plt.xlabel('$2s+d_0$ $(m)$',fontsize=20)
you can specify the axis limits. the problem is that autoscale is moving your two plots differently. if you insert 2 lines of code, each specifying the axis limits, it will fix it.
plt.axis([.030,.0415, 10, 17]) #line 17
plt.axis([.030,.0415, -.6, .8]) #line 26
i believe this is what you're looking for.
Try using GridSpec.
from matplotlib import gridspec
fig = plt.figure()
gs = gridspec.GridSpec(2, 1, height_ratios=[3, 1])
ax0 = plt.subplot(gs[0])
ax1 = plt.subplot(gs[1])
ax0.plot(x, m*x + b, '-', color='grey', alpha=0.5)
ax0.plot(x,y,'.',color='black',markersize=6)
ax1.plot(x, difference, 'ro')
And use set_ylabel instead of ylabel (which you use for plt for example) for axes.

Log plot with error bars in matplotlib doesn't work

I'm trying to create a plot with the following components:
Scatter plot
Line of best fit with error bars.
Y scaled to be log.
So this is a standard log linear plot saved to a png, but whilst I can get the scatter plot working I cannot get the fitted line to plot on the diagram. I just get one blob. Here is the code that I am using:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, xlim=(-2,2), ylim=(1,10E11))
ax.scatter(x, y, s=1, c='black')
line, = ax.semilogy([-0.5, 1], [-0.5*m+c, 1.0*m + c], color='red', linestyle='-', linewidth=2)
ax.errorbar(-0.5, -0.5*m+c, yerr=ser, marker='o', color='red')
ax.errorbar(1, m * 1.0 + c, yerr=ser, marker='o', color='green')
ax.set_yscale('log')
fig.savefig('log.png')
I get the scatter plot. and the log scale, but not the fitted line or the error bar.
x = np.array(x)
y = np.array(y)
~50,000 points
m = gradient = 2.38329162e+09
c = 1.24269722e+09
I've tried lots of variations, but I cannot seem to get the line plotted correctly. I cannot find one example of an error bar plot with log scale.
As an update, I could finally get the line working. It was due to the y heading below zero. However I cannot still get the error bars plotted. I only can get one whisker line plot (not four) and no horizontal joining lines.
matplotlib version: 1.2.0
Since you did not provide any number, I had to guess.
But this works, so your data might be weird (have you zoomed in to see if ser is not just really small?)
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(500,1)*2 -1
y = np.random.rand(500,1)*1e10
m = gradient = 2.38329162e+09
c = 1.24269722e+09
ser = 1e10
fig = plt.figure()
ax = fig.add_subplot(111, xlim=(-2,2), ylim=(1,10E11))
ax.scatter(x, y, s=1, c='black')
ax.plot([-1, 1], [m * -1.0 + c, 1.0*m + c], color='red', linestyle='-', linewidth=2)
ax.errorbar(-1, m * -1.0 + c, yerr=(ser), marker='o', color='green')
ax.errorbar(1, m * 1.0 + c, yerr=(ser), marker='o', color='green')
ax.set_yscale('log')
fig.savefig('log.png')
Result:

changing axis weight in matplotlib

How to change axis weight in matplotlib (make the axis much bolder)?
from pylab import *
x = [5,7,5,9,11,14]
y = [4,5,3,11,15,14]
scatter(x, y, s=50, color='green',marker='h')
show()
You can set the width of whats called a spine (a side of the axes) in Matplotlib:
fig, ax = plt.subplots()
ax.plot(np.random.randn(100).cumsum())
# The spines
plt.setp(ax.spines.values(), linewidth=3)
# The ticks
ax.xaxis.set_tick_params(width=3)
ax.yaxis.set_tick_params(width=3)
Use axhline, axvline:
axhline(linewidth=5, color='black')
axvline(linewidth=5, color='black')
axhline(linewidth=5, y=max(y)*1.1, color='black')
axvline(linewidth=5, x=max(x)*1.1, color='black')

Categories

Resources