Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用matplotlib高亮显示多个条形图_Python_Matplotlib_Graph - Fatal编程技术网

Python 如何使用matplotlib高亮显示多个条形图

Python 如何使用matplotlib高亮显示多个条形图,python,matplotlib,graph,Python,Matplotlib,Graph,我想突出显示最大值和其他两个条。谁能帮我做这个,谢谢 #Figure size plt.figure(figsize = (20, 8)) #Group by position and find the mean salary df.groupby("Position")["Salary"].mean() #Plot bar graph ax = df.groupby("Position")["Salary"].

我想突出显示最大值和其他两个条。谁能帮我做这个,谢谢

#Figure size
plt.figure(figsize = (20, 8))

#Group by position and find the mean salary
df.groupby("Position")["Salary"].mean()

#Plot bar graph
ax = df.groupby("Position")["Salary"].mean()

#Highlight max value bar
ax.plot.bar(color=np.where(ax==ax.max(), '#ff9999','cadetblue'))

#Axis label
plt.xlabel("Position",fontsize=12)
plt.ylabel("Salary",fontsize=12)
plt.title("Wages for different job functions", fontweight='bold', fontsize=14)

plt.show()

获取条形图数组并设置所需位置的颜色。在下面的示例中,除了为代码中的最大值设置特殊颜色外,还将第四个设置为红色

import matplotlib.pyplot as plt
import numpy as np

# Fixing random state for reproducibility
np.random.seed(19680801)

plt.rcdefaults()
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))

bars = ax.bar(y_pos, performance, align='center', color=np.where(performance == performance.max(),'#ff9999','cadetblue'))
ax.set_xticks(y_pos)
ax.set_xticklabels(people)
ax.set_ylabel('Salary')
ax.set_title("Wages for different job functions", fontweight='bold', fontsize=14)

bars[3].set_color("red")

plt.show()

我回答的依据是对官方文件中样本的修改。示例图形是一个水平条形图,因此请注意代码中x轴和y轴的名称是错误的。我得到了这个错误:TypeError:“AxesSubplot”对象不可下标。你知道原因吗?你是用我的代码运行的吗?在您的代码中,ax是数据帧的名称,并且您正在熊猫图中绘制图形。在我的代码的matplotlib中,ax是一个图形对象,所以我认为这就是原因。