Ticklabels inside a plot in matplotlib

Vlad

I have a barchart, where the tick labels on the 'y' axes are outside. Example However, I would like to have the ylabels inside the bars with an align to the left. Is there a way to do this? Here is the code:

fig, ax = plt.subplots()

# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))

ax.barh(y_pos, performance, xerr=error, align='center',
        color='green', ecolor='black')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis()  # labels read top-to-bottom
ax.set_xlabel('Performance')
ax.set_title('How fast do you want to go today?')

plt.show()

If I play with tick_params:

ax.tick_params(axis='y', direction='in',pad=-5)

I get

this

which is not what I want since the text is underneath and still aligned to the right.

ImportanceOfBeingErnest

This is a matter of setting the correct parameters.

  • Alignment is set for the ticklabels,

    ax.set_yticklabels(people, horizontalalignment = "left")
    
  • Padding needs to be much larger, e.g. pad = -15 to have the label inside the axes.

This would already give you the desired graph.

enter image description here

However, since you seem to be using the seaborn-darkgrid style the labels are behind the bars. To bring them in front you can set,

plt.rcParams["axes.axisbelow"] = "line"

Additionally it might make sense to turn the y grid off, which would otherwise strike through the textlabels.

plt.rcParams["axes.grid.axis"] ="x"

enter image description here

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Matplotlib plot settings

分類Dev

Matplotlib: Check for empty plot

分類Dev

Polar plot in Matplotlib not centered

分類Dev

条件に基づいてmatplotlibののticklabelsの色を変更します

分類Dev

Plot a vertical line using matplotlib

分類Dev

Plot the last 100 points in matplotlib

分類Dev

Matplotlib plot time overlapping labels

分類Dev

Determine plot size with grid Matplotlib

分類Dev

Determine plot size with grid Matplotlib

分類Dev

Unwanted labels on bar plot with matplotlib

分類Dev

Set plot title inside loop

分類Dev

matplotlib Axes.plot()とpyplot.plot()

分類Dev

matplotlib scatter plot with different markers and colors

分類Dev

python matplotlib plot table with multiple headers

分類Dev

matplotlib - multiple markers for a plot based on additional data

分類Dev

How to save a greyscale matplotlib plot to numpy array

分類Dev

matplotlib2.0.2のPlot_trisurf

分類Dev

Colorbar for matplotlib plot_surface using facecolors

分類Dev

matplotlib two legends out of bar plot

分類Dev

How to add a legend to matplotlib scatter plot

分類Dev

Matplotlib: how to set a tick label above a plot

分類Dev

Matplotlib plot multiple bars in one graph

分類Dev

How to repeat a plot in different subplots in matplotlib?

分類Dev

matplotlib histogram plot 'density' argument not working

分類Dev

Plot death count against date with Matplotlib

分類Dev

matplotlib shift pcolormesh plot to symmetrized coordinates

分類Dev

How to generate several legends for single plot matplotlib

分類Dev

plot errorbar with matplotlib based on multiindex pandas dataframe

分類Dev

Matplotlib regression scattered plot using Python?

Related 関連記事

ホットタグ

アーカイブ