Set the xticklabels color individually with matplotlib

0. What to achieve

xticklabels_example.png Set the color of xtick labels (America or Japan part) individually.

1.Code

example.ipynb


import matplotlib.pyplot as plt

#Drawing area settings
fig, ax = plt.subplots() 

#xticklabels and yvalue settings(list)
X = ["America", "Japan"]
Y = [3, 1]

#color abbreviation is OK
X_label_colors = ['r', 'green']


plt.bar(X, Y, align="center")

#Associate Xtick labels with color(Must be equal)
[t.set_color(i) for (i, t) in zip(X_label_colors, ax.xaxis.get_ticklabels())]

plt.show()

2. Note

Of the several labels, it can be used when there is a label that you want to stand out.

example2.ipynb


fig, ax = plt.subplots()

X = ["C/C++", "Python", "JavaScript"]
X_label_colors = ['black', 'red', 'black']
Y = [136, 127,110]

plt.bar(X, Y, align="center")

[t.set_color(i) for (i, t) in zip(X_label_colors, ax.xaxis.get_ticklabels())]

plt.show()

xticklabels_example2.png

Recommended Posts

Set the xticklabels color individually with matplotlib
[Python] Set the graph range with matplotlib
Gradient color selection with matplotlib
Color the integration interval with matplotlib.pyplot
Continuously color with matplotlib scatter plot
Specify the color in the matplotlib 2D map
Adjust the spacing between figures with Matplotlib
Align the size of the colorbar with matplotlib
Display markers above the border with matplotlib
Match the colorbar to the figure with matplotlib
Drawing tips with matplotlib on the server side
Animation with matplotlib
Japanese with matplotlib
The basis of graph theory with matplotlib animation
Animation with matplotlib
Align Matplotlib graph colors with similar colors (color map)
Visualize the behavior of the sorting algorithm with matplotlib
Histogram with matplotlib
Animate with matplotlib
matplotlib: Replace the axis itself with another one.
Let's apply the brand image color to the matplotlib colormap!
Add information to the bottom of the figure with Matplotlib
The background color of the QWidget subclass cannot be set
Color list that can be set with tkinter (memorial)
A story that struggled with the common set HTTP_PROXY = ~
Fill the background with a single color with OpenCV2 + Python
Display the graph while changing the parameters with PySimpleGUI + Matplotlib
Adjust the ratio of multiple figures with the matplotlib gridspec
2-axis plot with Matplotlib
Heatmap with Python + matplotlib
Band graph with matplotlib
Learn with Cheminformatics Matplotlib
Real-time drawing with matplotlib
Various colorbars with Matplotlib
3D plot with matplotlib
How to plot a lot of legends by changing the color of the graph continuously with matplotlib
Adjust axes with matplotlib
matplotlib Color list creation
[Python] Read the csv file and display the figure with matplotlib
Reformat the timeline of the pandas time series plot with matplotlib
Let's visualize the number of people infected with coronavirus with matplotlib
Reload the server set up with gunicorn when changing the code
Read the VTK file and display the color map with jupyter.
I wrote the basic operation of matplotlib with Jupyter Lab
The story that the private key is set to 600 with chmod