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'.
Related
I am trying to plot two separate quantities on the same graph using twiny as follows:
fig = figure()
ax = fig.add_subplot(111)
ax.plot(T, r, 'b-', T, R, 'r-', T, r_geo, 'g-')
ax.set_yscale('log')
ax.annotate('Approx. sea level', xy=(Planet.T_day*1.3,(Planet.R)/1000), xytext=(Planet.T_day*1.3, Planet.R/1000))
ax.annotate('Geostat. orbit', xy=(Planet.T_day*1.3, r_geo[0]), xytext=(Planet.T_day*1.3, r_geo[0]))
ax.set_xlabel('Rotational period (hrs)')
ax.set_ylabel('Orbital radius (km), logarithmic')
ax.set_title('Orbital charts for ' + Planet.N, horizontalalignment='center', verticalalignment='top')
ax2 = ax.twiny()
ax2.plot(v,r,'k-')
ax2.set_xlabel('Linear speed (ms-1)')
show()
and the data is presented fine, but I am having the problem that the figure title is overlapping with the axes labels on the secondary x axis so that it's barely legible (I wanted to post a picture example here, but I don't have a high enough rep yet).
I'd like to know if there's a straightforward way to just shift the title directly up a few tens of pixels, so that the chart looks prettier.
I'm not sure whether it is a new feature in later versions of matplotlib, but at least for 1.3.1, this is simply:
plt.title(figure_title, y=1.08)
This also works for plt.suptitle(), but not (yet) for plt.xlabel(), etc.
Forget using plt.title and place the text directly with plt.text. An over-exaggerated example is given below:
import pylab as plt
fig = plt.figure(figsize=(5,10))
figure_title = "Normal title"
ax1 = plt.subplot(1,2,1)
plt.title(figure_title, fontsize = 20)
plt.plot([1,2,3],[1,4,9])
figure_title = "Raised title"
ax2 = plt.subplot(1,2,2)
plt.text(0.5, 1.08, figure_title,
horizontalalignment='center',
fontsize=20,
transform = ax2.transAxes)
plt.plot([1,2,3],[1,4,9])
plt.show()
I was having an issue with the x-label overlapping a subplot title; this worked for me:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 1)
ax[0].scatter(...)
ax[1].scatter(...)
plt.tight_layout()
.
.
.
plt.show()
before
after
reference:
https://matplotlib.org/users/tight_layout_guide.html
ax.set_title('My Title\n', fontsize="15", color="red")
plt.imshow(myfile, origin="upper")
If you put '\n' right after your title string, the plot is drawn just below the title. That might be a fast solution too.
You can use pad for this case:
ax.set_title("whatever", pad=20)
Just use plt.tight_layout() before plt.show(). It works well.
A temporary solution if you don't want to get into the x, y position of your title.
Following worked for me.
plt.title('Capital Expenditure\n') # Add a next line after your title
kudos.
Using the plt.tight_layout() before the plt.show() works for me well.
you can even make it better and visible by adding a padding
ax.set_title("title", pad=15)
I have found similar questions previously, but I haven't managed to find an answer that has worked for me.
I am plotting directly from my data frame and would like to label my axis. This is the code I am using:
fig,ax = plt.subplots()
ax = plt.gca()
ax.set_xlabel("Time (s)")
ax.set_ylabel("Normalised Vertical Acceleration")
data.plot(kind='line', x='time', y='accel_y', ax=ax)
The graph generated only has "cycle" as the x-axis label and no y-axis label. Is there something that I'm doing wrong? Or is there a better method?
Thanks in advance.
Edited answer: Based on your updated question
You don't need additionally ax = plt.gca(). Then, first plot the data and then set the axis labels
fig, ax = plt.subplots()
data.plot(kind='line', x='time', y='accel_y', ax=ax)
ax.set_xlabel("Time (s)")
ax.set_ylabel("Normalised Vertical Acceleration")
#Sheldore's answer didn't work for me.
Instead, we need to use the inbuild function parameter:
fig, ax = plt.subplots()
data.plot(kind='line', x='time', y='accel_y', ax=ax,
ylabel="Normalised Vertical Acceleration",
xlabel="Time (s)")
I would report this as a bug to Pandas, which might be version-specific.
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 need to add a semi transparent skin over my matplotlib figure. I was thinking about adding a rectangle to the figure with alpha <1 and a zorder high enough so its drawn on top of everything.
I was thinking about something like that
figure.add_patch(Rectangle((0,0),1,1, alpha=0.5, zorder=1000))
But I guess rectangles are handled by Axes only. is there any turn around ?
Late answer for others who google this.
There actually is a simple way, without phantom axes, close to your original wish. The Figure object has a patches attribute, to which you can add the rectangle:
fig, ax = plt.subplots(nrows=1, ncols=1)
ax.plot(np.cumsum(np.random.randn(100)))
fig.patches.extend([plt.Rectangle((0.25,0.5),0.25,0.25,
fill=True, color='g', alpha=0.5, zorder=1000,
transform=fig.transFigure, figure=fig)])
Gives the following picture (I'm using a non-default theme):
The transform argument makes it use figure-level coordinates, which I think is what you want.
You can use a phantom axes on top of your figure and change the patch to look as you like, try this example:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
ax.set_zorder(1000)
ax.patch.set_alpha(0.5)
ax.patch.set_color('r')
ax2 = fig.add_subplot(111)
ax2.plot(range(10), range(10))
plt.show()
If you aren't using subplots, using gca() will work easily.
from matplotlib.patches import Rectangle
fig = plt.figure(figsize=(12,8))
plt.plot([0,100],[0,100])
plt.gca().add_patch(Rectangle((25,50),15,15,fill=True, color='g', alpha=0.5, zorder=100, figure=fig))
I am trying to plot two separate quantities on the same graph using twiny as follows:
fig = figure()
ax = fig.add_subplot(111)
ax.plot(T, r, 'b-', T, R, 'r-', T, r_geo, 'g-')
ax.set_yscale('log')
ax.annotate('Approx. sea level', xy=(Planet.T_day*1.3,(Planet.R)/1000), xytext=(Planet.T_day*1.3, Planet.R/1000))
ax.annotate('Geostat. orbit', xy=(Planet.T_day*1.3, r_geo[0]), xytext=(Planet.T_day*1.3, r_geo[0]))
ax.set_xlabel('Rotational period (hrs)')
ax.set_ylabel('Orbital radius (km), logarithmic')
ax.set_title('Orbital charts for ' + Planet.N, horizontalalignment='center', verticalalignment='top')
ax2 = ax.twiny()
ax2.plot(v,r,'k-')
ax2.set_xlabel('Linear speed (ms-1)')
show()
and the data is presented fine, but I am having the problem that the figure title is overlapping with the axes labels on the secondary x axis so that it's barely legible (I wanted to post a picture example here, but I don't have a high enough rep yet).
I'd like to know if there's a straightforward way to just shift the title directly up a few tens of pixels, so that the chart looks prettier.
I'm not sure whether it is a new feature in later versions of matplotlib, but at least for 1.3.1, this is simply:
plt.title(figure_title, y=1.08)
This also works for plt.suptitle(), but not (yet) for plt.xlabel(), etc.
Forget using plt.title and place the text directly with plt.text. An over-exaggerated example is given below:
import pylab as plt
fig = plt.figure(figsize=(5,10))
figure_title = "Normal title"
ax1 = plt.subplot(1,2,1)
plt.title(figure_title, fontsize = 20)
plt.plot([1,2,3],[1,4,9])
figure_title = "Raised title"
ax2 = plt.subplot(1,2,2)
plt.text(0.5, 1.08, figure_title,
horizontalalignment='center',
fontsize=20,
transform = ax2.transAxes)
plt.plot([1,2,3],[1,4,9])
plt.show()
I was having an issue with the x-label overlapping a subplot title; this worked for me:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 1)
ax[0].scatter(...)
ax[1].scatter(...)
plt.tight_layout()
.
.
.
plt.show()
before
after
reference:
https://matplotlib.org/users/tight_layout_guide.html
ax.set_title('My Title\n', fontsize="15", color="red")
plt.imshow(myfile, origin="upper")
If you put '\n' right after your title string, the plot is drawn just below the title. That might be a fast solution too.
You can use pad for this case:
ax.set_title("whatever", pad=20)
Just use plt.tight_layout() before plt.show(). It works well.
A temporary solution if you don't want to get into the x, y position of your title.
Following worked for me.
plt.title('Capital Expenditure\n') # Add a next line after your title
kudos.
Using the plt.tight_layout() before the plt.show() works for me well.
you can even make it better and visible by adding a padding
ax.set_title("title", pad=15)