How to show the vertical scale of marginal histogram in a jointplot - python

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.

Related

Position bars between tick marks (and not on tick marks) in plotly

I am using Plotly and Python to chart a bar plot. On the x-axis, Plotly arranges the values from each trace around the centre of the tick mark.
This is what I am getting now:
I would like to have the data points (and labels) in between the tick marks. In the example chart, this would mean all the bars centered around 0-2kw would move left of the first tick and the label centered, all the bars around 2-4kw would move between the first and second ticks and the label centered, etc..
I am using tickmode=array, ticktext as an array and also set tickson=boundaries, but it's still the same.
Is there a way to do this?
(Not sure if this makes any difference but there are multiple charts in subplots)
Answering my own question.
Normally setting tickon=boundaries should do the trick, but it doesn't seem to work in conjunction with tickmode=array and ticktext.
The solution for me was to create the labels array and provide it to the bar chart as the x parameter, something similar to this:
fig = go.Figure(data=go.Bar(name='Trace1', x=['0-2kw', '2-4kw', '4-6kw', '6-8kw'], y=[0.2, 0.2, 0.2, 0.4]))
fig.update_xaxes(showgrid=True, tickson='boundaries')
Doing this in my code now the data bars are in between the grid lines:

How to rotate y-axis tick labels in Pandas barplot

For Pandas.DataFrame plot() fn, the 'rot' keyword argument rotates the x-axis ticks specifically. So how does one rotate the y-axis ticks?
There's some documentation here, which gives the anticipated syntax for the 'rot' argument for either xticks or yticks, but lacking of an example.
If you want to rotate axes ticks, it's convenient to use matplotlib feature.
After importing matplotlib as 'plt', you just have to write:
plt.xticks(rotation=specify_here_a_value)
plt.yticks(rotation=specify_here_a_value)
plt.show()
It will do the work for you. Coming to the question you asked, 'rot' keyword takes argument or rotates the axis based on the type of graph as said in the documentation:
rot : int, default None
Rotation for ticks (xticks for vertical, yticks for horizontal plots)
I think if plot is vertical, it rotates xticks and if it is horizontal it rotates yticks.
Matplotlib provides the better way as written above. I find it much more effective.
Example
axs= pd.plotting.scatter_matrix(numericData, figsize = (colNo,colNo))
for i in range(colNo):
vert = axs[i,0]
vert.yaxis.label.set_rotation(0)
vert.xaxis.label.set_ha('right')
vert.set_yticks(())
h = axs[colNo-1, i]
h.xaxis.label.set_rotation(90)
h.set_xticks(())

Is there a way to make gridline labels appear on only one set of axes?

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

Seaborn PairPlot rotate x tick labels. Categorical data labels are overlapping

I'm trying to create plots which show the correlation of the "value" parameter to different categorical parameters. Here's what I have so far:
plot = sns.pairplot(df, x_vars=['country', 'tier_code', 'industry', 'company_size', 'region'], y_vars=['value'], height=10)
Which produces the following set of plots:
As you can see, the x axis is extremely crowded for the "country" and "industry" plots. I would like to rotate the category labels 90 degrees so that they wouldn't overlap.
All the examples for rotating I could find were for other kinds of plots and didn't work for the pairplot. I could probably get it to work if I made each plot separately using catplot, but I would like to make them all at once. Is that possible?
I am using Google Colab in case it makes any difference. My seaborn version number is 0.10.0.
Manish's answer uses the get_xticklabels method, which doesn't always play well with the higher level seaborn functions in my experience. So here's a solution avoiding that. Since I don't have your data, I'm using seaborn's tips dataset for an example.
I'm naming the object returned by sns.pairplot() grid, just to remind us that it is a PairGrid instance. In general, its axes attribute yields a two-dimensional array of axes objects, corresponding to the subplot grid. So I'm using the flat method to turn this into a one-dimensional array, although it wouldn't be necessary in your special case with only one row of subplots.
In my example I don't want to rotate the labels for the third subplot, as they are single digits, so I slice the axes array accordingly with [:2].
import seaborn as sns
sns.set()
tips = sns.load_dataset("tips")
grid = sns.pairplot(tips, x_vars=['sex', 'day', 'size'], y_vars=['tip'])
for ax in grid.axes.flat[:2]:
ax.tick_params(axis='x', labelrotation=90)
You can rotate x-axis labels as:
plot = sns.pairplot(df, x_vars=['country', 'tier_code', 'industry', 'company_size', 'region'],
y_vars=['value'], height=10)
rotation = 90
for axis in plot.fig.axes: # get all the axis
axis.set_xticklabels(axis.get_xticklabels(), rotation = rotation)
plot.fig.show()
Hope it helps.

Preventing xticks from overlapping yticks

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.

Categories

Resources