2024-05-16 21:04:36 发布
网友
我正在使用command.plot(kind=“bar”)制作一个条形图,按国家划分的财富500强公司。当我得到一个条形图时,我只想突出显示那些箱子,这些箱子给了我中国和美国的公司数量达到500家,以便更好地突出差异。无论如何,有没有办法让这两个箱子的颜色不同,让它们脱颖而出
使用pandas df.plot(kind='bar'),您可能需要通过迭代生成的条来设置颜色。使用seaborn,您可以直接设置:
df.plot(kind='bar')
from matplotlib import pyplot as plt import pandas as pd import numpy as np import seaborn as sns countries = ['China', 'India', 'United States', 'Indonesia', 'Pakistan', 'Brazil', 'Nigeria', 'Bangladesh', 'Russia', 'Mexico'] df = pd.DataFrame({'country': countries, 'number': np.random.randint(10, 30, len(countries))}) colors = ['dodgerblue' if cntry == 'United States' else 'limegreen' if cntry == 'China' else 'turquoise' for cntry in df['country']] sns.barplot(x='country', y='number', data=df, palette=colors) plt.show()
为了实现与熊猫相似的效果,假设没有其他“斑块”元素被绘制到同一子地块上:
ax = df.plot(kind='bar') for bar, color in zip(ax.patches, colors): bar.set_color(color)
请注意,熊猫确实允许在绘制多个数据帧列时为df.plot(kind='bar')提供多种颜色。因此,每个打印列都有一种颜色
使用pandas
df.plot(kind='bar')
,您可能需要通过迭代生成的条来设置颜色。使用seaborn,您可以直接设置:为了实现与熊猫相似的效果,假设没有其他“斑块”元素被绘制到同一子地块上:
请注意,熊猫确实允许在绘制多个数据帧列时为
df.plot(kind='bar')
提供多种颜色。因此,每个打印列都有一种颜色相关问题 更多 >
编程相关推荐