Is there a way to define a distance between the ticks and the spine on a line plot? I have managed to create the following (yticks are separated from the spine and the grid lines)
With the following code:
params_1 = {
'axes.spines.top': False,
'axes.spines.left': False,
'axes.spines.right': False,
'ytick.major.size': 10,
}
plt.rcParams.update(params_1)
fig = plt.figure(figsize=(13,6.5))
ax = fig.add_subplot(111, facecolor='w')
ax.set_ylim([5,15])
ax.set_xticks([5,10,15,20,25])
ax.yaxis.grid()
ax_xgrid = ax.xaxis.grid(linestyle=':', linewidth=1.5)
[i.set_marker('o') for i in ax.yaxis.get_ticklines()]
[i.set_markeredgecolor('w') for i in ax.yaxis.get_ticklines()]
[i.set_markeredgewidth(4) for i in ax.yaxis.get_ticklines()]
This looks is exactly what I would like but if is save the figure with transparency then I see the white circles around the yticks. Any ideas how to solve this?
Thanks,
If I understand correctly what you are asking, this can be easily done using the tick_params() helper function.
fig, ax = plt.subplots()
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.tick_params(axis='y', color='none', pad=50)
ax.grid()
plt.show()
EDIT I did not understand what you were trying to do. What you want to increase the distance between the left axis and the main part of the plot. To do so, use the Spine.set_position() function. The following should work:
fig, ax = plt.subplots()
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['left'].set_position(('outward', 20))
[i.set_marker('o') for i in ax.yaxis.get_ticklines()]
ax.grid()
plt.show()
Related
I am working on a multi axes plot. I have troubble in setting the ylims of each sub plot.
I am not providing the data as the problem is related to setting the axes only.
Here is the code:
ax1 = plt.subplot(411)
plt.plot(x,y1,'-r')
ax2 = plt.subplot(412,sharex=ax1)
plt.plot(x,y2,'-g')
ax3 = plt.subplot(413,sharex=ax1)
plt.plot(x,y3,'-k')
ax4 = plt.subplot(414,sharex=ax1)
plt.plot(x,y4,'-b')
ax1.get_shared_x_axes().join(ax1,ax2,ax3,ax4)
#make x axis on upper invisible
plt.setp(ax1.get_xaxis(), visible=False)
plt.setp(ax2.get_xaxis(), visible=False)
ax1.spines['bottom'].set_visible(False)
ax1.set_ylim([20,40])
ax2.spines['top'].set_visible(False)
ax2.spines['bottom'].set_visible(False)
ax2.set_ylim([0,0.8])
ax3.spines['top'].set_visible(False)
ax3.spines['bottom'].set_visible(False)
ax4.spines['top'].set_visible(False)
ax1.grid(axis="y")
ax2.grid(axis="y")
ax3.grid(axis="y")
ax4.grid(axis="y")
plt.subplots_adjust(hspace=0.01)
ax4.set_xlabel('Time (s)')
plt.subplots_adjust(left=0.12, right=0.97, top=0.95, bottom=0.15)
plt.show()
Existing output: We see the ylim of subplot1 has been reset. But, ax2 and ax3 y limits did not change.
From #QuangHoang in the comments:
I think your code work, notice how 0.5 is not on top of ax2, that small distance would account for the space from 0.5 to 0.8.
can someone explain why this simple code wont execute the facecolor command while setting the axis off?
fig = plt.figure(1)
ax = fig.add_subplot(211, facecolor=(0,0,0), aspect='equal')
ax.scatter(np.random.random(10000), np.random.random(10000), c="gray", s=0.25)
ax.axes.set_axis_off()
Thanks in advance!
The background patch is part of the axes. So if the axes is turned off, so will the background patch.
Some options:
Re-add the background patch
ax = fig.add_subplot(211, facecolor=(0,0,0), aspect='equal')
ax.set_axis_off()
ax.add_artist(ax.patch)
ax.patch.set_zorder(-1)
Create new patch
ax = fig.add_subplot(211, facecolor=(0,0,0), aspect='equal')
ax.set_axis_off()
ax.add_patch(plt.Rectangle((0,0), 1, 1, facecolor=(0,0,0),
transform=ax.transAxes, zorder=-1))
Turn axis spines and ticks invisible
...but keep the axis on.
ax = fig.add_subplot(211, facecolor=(0,0,0), aspect='equal')
for spine in ax.spines.values():
spine.set_visible(False)
ax.tick_params(bottom=False, labelbottom=False,
left=False, labelleft=False)
I'm trying to plot a smoother grid in the background of this grid that's already plotted. This is what I've done so far. The grid follows my major ticks. I'd like this smoother grid to follow the minor ticks. Is this possible?
My code until now:
fig, ax = plt.subplots(figsize = (20,10))
ax.set_xticks(np.arange(0,round(max(datax)+1)))
ax.set_yticks(np.arange(0,round(max(datay)+1),step = 0.1))
ax.minorticks_on()
ax.grid(True)
plt.xlabel("Tensão (V)", fontsize = 14)
plt.ylabel("Corrente (mA)", fontsize = 14)
plt.title("Experimento 2", fontsize = 20)
ax.errorbar(datax,datay,xerr = sigmax, yerr = sigmay, fmt = ',')
ax.set(xlim= -1, ylim = 0)
P.S.: would you guys organize this code differently? I think it's a complete mess.
i want my grids to look like this
this is how they are now
What you want is the linestyle keyword argument for grid, along with the linewidth keyword argument.
Here's how you can use dotted lines for your grid, with thinner lines for the minor ticks:
ax.grid(True, which='major', linestyle=':', linewidth=1, color="black")
ax.grid(True, which='minor', linestyle=':', linewidth=0.5, color="black")
Here's the output (I used faked data since you did not provide a MWE):
You can fiddle with the linewidth parameter to have the lines appear thinner, or on the color to make them fainter.
You can also try other linestyles out, like dashed (linestyle='--').
How do I show a plot with twin axes such that the aspect of the top and right axes are 'equal'. For example, the following code will produce a square plot
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.plot([0,1],[0,1])
But this changes as soon as you use the twinx function.
ax2 = ax.twinx()
ax2.set_ylim([0,2])
ax3 = ax.twiny()
ax3.set_xlim([0,2])
Using set_aspect('equal') on ax2 and ax3 seems to force it the the aspect of ax, but set_aspect(0.5) doesn't seem to change anything either.
Put simply, I would like the plot to be square, the bottom and left axes to run from 0 to 1 and the top and right axes to run from 0 to 2.
Can you set the aspect between two twined axes? I've tried stacking the axes:
ax3 = ax2.twiny()
ax3.set_aspect('equal')
I've also tried using the adjustable keyword in set_aspect:
ax.set_aspect('equal', adjustable:'box-forced')
The closest I can get is:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_aspect('equal', adjustable='box-forced')
ax.plot([0,1],[0,1])
ax2=ax.twinx()
ax3 = ax2.twiny()
ax3.set_aspect(1, adjustable='box-forced')
ax2.set_ylim([0,2])
ax3.set_xlim([0,2])
ax.set_xlim([0,1])
ax.set_ylim([0,1])
Which produces:
I would like to remove the extra space to the right and left of the plot
It seems overly complicated to use two different twin axes to get two independent set of axes. If the aim is to create one square plot with one axis on each side of the plot, you may use two axes, both at the same position but with different scales. Both can then be set to have equal aspect ratios.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.plot([0,1],[0,1])
ax2 = fig.add_axes(ax.get_position())
ax2.set_facecolor("None")
ax2.set_aspect('equal')
ax2.plot([2,0],[0,2], color="red")
ax2.tick_params(bottom=0, top=1, left=0, right=1,
labelbottom=0, labeltop=1, labelleft=0, labelright=1)
plt.show()
I am using matplotlib.pyplot to plot some graphs and for some reasons I can't see the lines of the axes, although I can see the xticks and yticks. Important to note that I am using python notebook, and usually I try to visualize my graphs with the function (%matplotlib inline)
Here is an example figure that I get (without the axes):
Here is the code I used to produce this figure:
fig, ax = plt.subplots(1,1, figsize=(7.5,6), sharey=False, sharex=False, edgecolor='k', frameon=True)
ax.plot(np.array(frequency_vec), before_LTP, 'b-o', label='Before');
ax.plot(np.array(frequency_vec), After_LTP, 'r-o', label='After');
plt.yticks([1,2,3,4,5,6,7,8], ['1','2','3','4','5','6','7','0'], fontsize=14)
plt.xticks(fontsize=14)
plt.rcParams['axes.edgecolor']='k'
ax.patch.set_visible(False)
ax.grid(False)
ax.set_frame_on(True)
ax.set_xlim(0, 110)
ax.set_ylim(1,(Number_of_pulses)+2)
ax.legend(loc='best', fontsize=15)
plt.xticks([12.5,25,50,75,100], ['12.5','25','50','75','100']);
So again - How can I make my axes-lines to be visible?
Thanks!
Do you have some special setting in your matplotlibrc file such as edgecolor?
import matplotlib as mpl
print mpl.rcParams['axes.edgecolor']
If it's 'w' (white) set it to 'k' (black)
If it's not edgecolor, do you have frameon = False? Try something like this:
fig, ax = subplots()
ax.plot([1,2,4],[4,5,6], 'r^-')
ax.set_frame_on(True)
I wrote that and it worked
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
Well, just write 'True' instead of 'False'.