matplotlib set yaxis label size - python

How can I change the size of only the yaxis label?
Right now, I change the size of all labels using
pylab.rc('font', family='serif', size=40)
but in my case, I would like to make the y-axis label larger than the x-axis. However, I'd like to leave the tick labels alone.
I've tried, for example:
pylab.gca().get_ylabel().set_fontsize(60)
but I only get:
AttributeError: 'str' object has no attribute 'set_fontsize'
So, obviously that doesn't work. I've seen lots of stuff for tick sizes, but nothing for the axis labels themselves.

If you are using the 'pylab' for interactive plotting you can set the labelsize at creation time with pylab.ylabel('Example', fontsize=40).
If you use pyplot programmatically you can either set the fontsize on creation with ax.set_ylabel('Example', fontsize=40) or afterwards with ax.yaxis.label.set_size(40).

Related

Plotly Python heatmap colorbar orientation

Is there a way to change the orientation of the colorbar in Plotly heatmap? Setting the orientation in the layout does not do anything:
go.Layout(
legend=dict(
orientation="h")
)
It doesn't even give me an error message. I have also tried change the orientation in the colorbar directly:
colorbar = dict(
orientation="h"
)
but I get the error message that 'orientation' is not a valid property of 'plotly.graph_objs.heatmap.ColorBar'.
I know how to set the position of the colorbar and I have looked the valid properties of the colorbar but could not find a way to set its orientation. Is this possible?
Set the orientation of the colorbar.
As per the Plotly Documentation:
# For the vertical orintation
fig.update_traces(colorbar_orientation='v', selector=dict(type='heatmap'))
# For the horizontal orintation
fig.update_traces(colorbar_orientation='h', selector=dict(type='heatmap'))
Default: "v" #vertical
I don't believe so. According to this open issue and the response from Chriddyp (co-founder of Plotly), a horizontal colorbar has not been implemented but this feature may be added in future releases of Plotly.
If you really need it— I suppose you can try to draw the colorbar yourself using annotations but that's admittedly a lot of work.

Change order of axes drawn in matplotlib figure

I have a figure in matplotlib where two axes overlap one-another. The axes draw according to the order in which they were created in the code, as shown when I print fig.axes, which returns [<matplotlib.axes._subplots.AxesSubplot object at 0x0000021C7417B320>, <matplotlib.axes._subplots.AxesSubplot object at 0x0000021C72371630>]. I would like to change the order so that the second axes created draws first. The property fig.axes is not writeable, so I unfortunately can't create a new list with the order I need and then assign it. I've also tried using ax.set_zorder() where I specified ax1.set_zorder(0) and ax2.set_zorder(1), but this did not work, and neither did the reverse or larger integer values. I can't seem to find anything in the documentation that would allow me to change the order in which axes are drawn, does anyone know of a way?
Purpose
As you can see, the grey year labels along the x-axis are covered by the black date call-outs. The year labels are part of ax1 and the date labels ax2. I'd like to switch the order so that ax1 is drawn above ax2, so as to not cover the year text.
Thanks for any help and suggestions!

Changing alpha value of ticklabels without specifying text

Matplotlib allows changing the alpha value of almost anything, but how does it work for an ticklabel?
If I have a text, it is easy:
ax.set_xticklabel(labels, alpha=alpha)
The case is different if I do not have a text as the following throws a TypeError, due to missing labels.
ax.set_xticklabel(alpha=alpha)
Therefore, my next idea was to get the automatically generated ticklabels and use them to do the job:
labels = [label.get_text() for label in ax.get_xticklabels()]
ax.set_xticklabels(labels, alpha=alpha
The problem here is, the labels are empty due to the dynamic nature of matplotlib (see here).
So, is there an easy way to change the alpha of my ticklabels without knowing the text beforehand?
It is probably not desirable to set the ticklabels themselves, if you want to change their color. The reason is that setting the labels via ax.set_ticklabels changes the formatter to a FixedFormatter; with this one would loose the automatic formatting behaviour.
Instead change the alpha of the text objects that later constitute the ticklabels. To this end plt.setp is a useful feature.
plt.setp(ax.get_xticklabels(), alpha=0.3)
The same can be achieved via
for t in ax.get_xticklabels():
t.set_alpha(0.3)

Why is set_xlim() not setting the x-limits in my figure?

I'm plotting some data with matplotlib. I want the plot to focus on a specific range of x-values, so I'm using set_xlim().
Roughly, my code looks like this:
fig=plt.figure()
ax=fig.add_subplot(111)
for ydata in ydatalist:
ax.plot(x_data,y_data[0],label=ydata[1])
ax.set_xlim(left=0.0,right=1000)
plt.savefig(filename)
When I look at the plot, the x range ends up being from 0 to 12000. This occurs whether set_xlim() occurs before or after plot(). Why is set_xlim() not working in this situation?
Out of curiosity, what about switching in the old xmin and xmax?
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(x_data,y_data)
ax.set_xlim(xmin=0.0, xmax=1000)
plt.savefig(filename)
The text of this answer was taken from an answer that was deleted almost immediately after it was posted.
set_xlim() limits the data that is displayed on the plot.
In order to change the bounds of the axis, use set_xbound().
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(x_data,y_data)
ax.set_xbound(lower=0.0, upper=1000)
plt.savefig(filename)
In my case the following solutions alone did not work:
ax.set_xlim([0, 5.00])
ax.set_xbound(lower=0.0, upper=5.00)
However, setting the aspect using set_aspect worked, i.e:
ax.set_aspect('auto')
ax.set_xlim([0, 5.00])
ax.set_xbound(lower=0.0, upper=5.00)
I have struggled a lot with the ax.set_xlim() and couldn't get it to work properly and I found out why exactly. After setting the xlim I was setting the xticks and xticklabels (those are the vertical lines on the x-axis and their labels) and this somehow elongated the axis to the needed extent. So if the last tick was at 300 and my xlim was set at 100, it again widened the axis to the 300 just to place the tick there.
So the solution was to put it just after the troublesome code:
ax.set_xlabel(label)
ax.set_xticks(xticks)
ax.set_xticklabels(xticks, rotation=60)
ax.set_xlim(xmin=0.0, xmax=100.0)
The same thing occurred to me today. My issue was that the data was not in the right format, i.e. not floats. The limits I set (itself floats) became meaningless compared to e.g. strings. After putting float() around the data, everything worked as expected.

Python graph- change axis marker colours and legend border colours

I'm using Python to plot a couple of graphs and I'm trying to change the formatting and essentially 'brand' the graph. I've managed to change most things using pylab.rcParams[...], but I can't work out how to change the colour of the markers on the axes and the border around the legend. Any help would be much appreciated. The line below is an example of the type of code I've been using to edit other parts. Basically just lines taken from matplotlibrc, but I can't find them to change everything I want.
pylab.rcParams[axes.labelcolor' = '#031F73'
If you just want to use rcParams, the proper parameters are xticks.color and yticks.color. I can't seem to find a key for the legend frame color. You can set that (along with the tick colors) programmatically though.
import pylab
pylab.plot([1,2,3],[4,5,6], label ='test')
lg = pylab.legend()
lg.get_frame().set_edgecolor('blue')
ax = pylab.axes()
for line in ax.yaxis.get_ticklines():
line.set_color('blue')
for line in ax.xaxis.get_ticklines():
line.set_color('blue')
for label in ax.yaxis.get_ticklabels():
label.set_color('blue')
for label in ax.xaxis.get_ticklabels():
label.set_color('blue')
pylab.show()

Categories