I'm using matplotlib 3.4.3
, ubuntu 20.04
, and Python 3.8
.I'm trying to change the label color with multicolors, but it shows black color without giving any error. I have installed all packages and
CodePudding user response:
This appears to be a known issue with many of the matplotlib backends. For example, see here:
Note: tested on python 3.7.10
, matplotlib 3.4.2
, MacOS 10.15.7
CodePudding user response:
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.transforms import Affine2D
import os
cwd = os.path.dirname(__file__)
def rainbow_text(x, y, strings, colors, orientation='vertical',
ax=None, **kwargs):
if ax is None:
ax = plt.gca()
t = ax.transData
canvas = ax.figure.canvas
assert orientation in ['horizontal', 'vertical']
if orientation == 'vertical':
kwargs.update(rotation=90, verticalalignment='bottom')
for s, c in zip(strings, colors):
text = ax.text(x, y, s " ", color=c, transform=t, **kwargs)
# Need to draw to update the text position.
text.draw(canvas.get_renderer())
ex = text.get_window_extent()
if orientation == 'horizontal':
t = text.get_transform() Affine2D().translate(ex.width, 0)
else:
t = text.get_transform() Affine2D().translate(0, ex.height)
df = pd.read_csv('emp.csv', error_bad_lines=False)
colors_ = ['black', 'red', 'black', 'red']
i=0
for row in df.itertuples(index=True, name='Pandas'):
plt.scatter(getattr(row, "sal"), getattr(row, "inc"), color = 'b', s=10)
word = ['First name=', str(getattr(row, "first_name")), 'Last name=', str(getattr(row, "last_name"))]
rainbow_text(df["sal"].min()-8.3, df["inc"].median()-1.2, word, colors_, size=5)
word = ['age=', str(getattr(row, "age")), 'gender=', str(getattr(row, "gender"))]
rainbow_text(df["sal"].min()-7.8, df["inc"].median()-1.2, word, colors_, size=5)
plt.savefig(os.path.join(cwd, 'fig/Test_fig_' str(i) '.pdf'), format='pdf', dpi=600, bbox_inches='tight')
i = 1
plt.show()