我正在使用matplotlib 3.4.3、ubuntu 20.04和 。我Python 3.8 試圖用多色更改標簽顏色,但它顯示黑色而沒有給出任何錯誤。我已經安裝了所有軟體包并
uj5u.com熱心網友回復:
這似乎是許多 matplotlib 后端的已知問題。例如,請參見此處:
注意:在python 3.7.10, matplotlib 3.4.2,上測驗MacOS 10.15.7
uj5u.com熱心網友回復:

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()

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/324251.html
標籤:蟒蛇-3.x matplotlib 乳胶
