I'm making a map using matplotlib.pyplot and I used the gridlines feature to create "labels" on the x and y axis of degrees latitude and longitude. I set the gridlines color to "none" to avoid having the gridlines there. However, these labels appear on each side of the plot and, at one point, coincide with my colorbar. Is there a way I could make these gridline labels only appear on the bottom and left of the plot? I can't find a list of the available kwargs anywhere. This is the code I used:
ax.gridlines(draw_labels=True, color="none")
And here is an image of the map. I would ideally like to remove the degree labels on the right and top axes.
You can achieve what you need with these relevant code:-
# minor change to the existing line of code
gls = ax.gridlines(draw_labels=True, color="none")
# other lines of code
# add these before plotting
gls.top_labels=False # suppress top labels
gls.right_labels=False # suppress right labels
The project I am doing requires code to plot more than 300 candlestick charts in several figures using mplfinance library. I am aware that this can only be done using external axes method as it provides more flexibilities and can plot unlimited charts theoretically.
The current code I am using is as below, the charts plotted can be seen below:
import mplfinance as mpf
s = mpf.make_mpf_style(base_mpf_style='yahoo', rc={'font.size': 6})
fig = mpf.figure(figsize=(34, 13.2), style=s, tight_layout=True)
ax_p = fig.add_subplot(n_rows, n_cols, pos_price)
ax_v = fig.add_subplot(n_rows, n_cols, pos_vol, sharex=ax_p)
fig, ax_list = mpf.plot(resampled_df, type='candle', ax=ax_p, volume=ax_v, show_nontrading=False,
datetime_format='%a %d-%m-%y', xrotation=0, returnfig=True)
The screenshot of the 6 sample charts from hundreds of charts my code plotted:
The screenshot of the two charts the above code plotted is as below:
As you can see the volume chart was plotted in an individual chart below the candlestick chart. I struggle to find the solution to move the volume into candlestick chart, there is a similar post in mplfinance documentation issue 114 kind of explains how to do this...... but I found it is rather difficult to understand for new ppl to the library like me.
Would highly appreciate it if you could post the detailed code to do this!
Update on 12th Feb 2021:
I modified the code with #Daniel's suggestion, use add_axes() rather than add_subplot() and now the volume is at the bottom of the candlestick chart when plotting multiple charts. Beautiful! Answer accepted.
ax_intra_day_candle = fig.add_axes([x_pos, y_pos, ax_width, ax_height])
ax_intra_day_candle.set_title(title)
ax_intra_day_volume = fig.add_axes([x_pos, y_pos - ax_vol_height, ax_width, ax_vol_height], sharex=ax_intra_day_candle)
mpf.plot(intra_day_df, type='candle', ax=ax_intra_day_candle, volume=ax_intra_day_volume, show_nontrading=False,
datetime_format='%a %m-%d', xrotation=0)
I will assume what you are asking is to have the volume and candlesticks share the same x-axis similar to this image here.
The simplest way to do this is to use fig.add_axes() (instead of fig.add_subplot())
In this way you can control exactly where in the Figure each Axes is placed. You can see this being done in the mplfinance code here.
The basic idea is that you specify the location of each Axes object in terms of a fraction of the total figure, indicating the lower left corner of the Axes, and its width and height.
When you want two Axes objects to touch, with no space between them, you specify the location and width/height accordingly so that the top of the lower Axes and the bottom of the upper Axes exactly meet.
So, for example, to stack two equally sized Axes exactly on top of each other, lets say in the upper left quadrant of the Figure you would have:
# ax = fig.add_axes([left,bottom,width,height])
ax1 = fig.add_axes([0.05,0.75,0.5,0.25])
ax2 = fig.add_axes([0.05,0.50,0.5,0.25])
The 0.05 space to the left allows room for the y-axis labels.
ax1 starts three quarters (0.75) of the way up from the bottom, and stretches half way (0.5) to the right with a height of 0.25 (which takes it to the very top of the Figure).
ax2 starts half way (0.50) up from the bottom, also stretches half way (0.5) across to the right, and has a height of 0.25 taking it exactly to the very bottom of ax1.
HTH
Here is a more specific example, and the result. Notice how the candles and volume plot together only take up the upper left quadrant of the figure:
fig = mpf.figure(figsize=(8,8),style='yahoo')
ax1 = fig.add_axes([0.05,0.75,0.5,0.25])
ax2 = fig.add_axes([0.05,0.50,0.5,0.25])
mpf.plot(df,type='candle',ax=ax1,volume=ax2)
mpf.show()
No need so long code use the below code
suppose you have volume and data for plotting in a chart using mplfinance
mpf.plot(data,type='candle',style='yahoo',volume=True)
In Seaborn jointplot, the marginal histograms do not show the y axis values. How can I get these values? The documentation doesn't show any arguments to change this behavior.
You're going to have to work more on the matplotlib side of things. If you just want to get the limits of the axis, you can use get_ylim. The handle for those histograms are ax_marg_x and ax_marg_y.
g = sns.jointplot(...)
g.ax_marg_x.get_ylim()
You can also make the tick labels visible using set_visible on the tick labels:
for tick in g.ax_marg_x.get_yticklabels():
tick.set_visible(True)
You can also create your own tick labels with set_yticklabels.
I have been trying to plot a scatterplot matrix using the great example given by Joe Kington:
However, I would like to add xlabels and ylabels on the subplots where I have displayed ticks. When you change the positions of the ticks, the associated x/ylabel does not follow.
I have not been able to find an option to change the location of the label; I was hoping to find something like ax.set_xlabel('XLabel',position='top') but it does not exist.
This is what I get finally,
For example I would like X axis4 to be above the ticks.
If you want to change the x-label from bottom to top, or the y-label from left to right, you can (provided the specific suplot is called ax) do it by calling:
ax.xaxis.set_label_position('top')
ax.yaxis.set_label_position('right')
If you for example want the label "X label 2" to stay where it is but don't overlap the other subplot, you can try to add a
fig.tight_layout()
just before the fig.show().
How can I prevent the labels of xticks from overlapping with the labels of yticks when using hist (or other plotting commands) in matplotlib?
There are several ways.
One is to use the tight_layout method of the figure you are drawing, which will automatically try to optimize the appareance of the labels.
fig, ax = subplots(1)
ax.plot(arange(10),rand(10))
fig.tight_layout()
An other way is to modify the rcParams values for the ticks formatting:
rcParams['xtick.major.pad'] = 6
This will draw the ticks a little farter from the axes. after modifying the rcparams (this of any other, you can find the complete list on your matplotlibrc configuration file), remember to set it back to deafult with the rcdefaults function.
A third way is to tamper with the axes locator_params telling it to not draw the label in the corner:
fig, ax = subplots(1)
ax.plot(arange(10),rand(10))
ax.locator_params(prune='lower',axis='both')
the axis keywords tell the locator on which axis it should work and the prune keyword tell it to remove the lowest value of the tick
Try increasing the padding between the ticks on the labels
import matplotlib
matplotlib.rcParams['xtick.major.pad'] = 8 # defaults are 4
matplotlib.rcParams['ytick.major.pad'] = 8
same goes for [x|y]tick.minor.pad.
Also, try setting: [x|y]tick.direction to 'out'. That gives you a little more room and helps makes the ticks a little more visible -- especially on histograms with dark bars.