python - 仅具有高亮值的 matplotlib 条形图

标签 python pandas matplotlib data-visualization visualization

您好,我想要这种条形图。问题是如何通过选择设置相应的xlables?

enter image description here

我编码如下以删除不需要的国家/地区标签,但该图也有 nan 作为标签。

countries=['United States','Mexico','Japan','China','Korea,Rep.','Ireland','France','Italy']
new_index=list(df.index)
for i in range(len(new_index)):
    if new_index[i] not in countries :
        new_index[i]=np.nan

这是我的结果,标签中有 nan,条形之间的距离更宽: enter image description here

对于数据:

import numpy as np
import pandas as pd

#Overall Country list
Countries=['United States','Mexico','Japan','China','Korea,Rep.','Ireland','France','Italy','Czech Republic',
 'Austria',
 'Slovak Republic',
 'Slovenia',
 'Germany',
 'Portugal',
 'Hungary',
 'Colombia',
 'New Zealand',
 'Norway',
 'Latvia']

#Countries to highlight
Desired=['United States','Mexico','Japan','China','Korea,Rep.','Ireland','France','Italy']

np.random.seed(0)
Value=np.random.rand(len(Countries))
df = pd.DataFrame({'Countries': Countries,'Value': Value,})
df.sort_values(['Value'],inplace=True)

df.set_index('Countries',drop=True,inplace=True)
ax_1 = df['Value'].plot(kind='bar', title ="graph", figsize=(10, 6), fontsize=12)
ax_1.set_xlabel("Country Name", fontsize=12)
plt.show()

最佳答案

运行 x-ticks,然后根据 countries 列表禁用其中的一些。

import numpy as np
import pandas as pd

#Overall Country list
Countries=['United States','Mexico','Japan','China','Korea,Rep.','Ireland','France','Italy','Czech Republic',
 'Austria',
 'Slovak Republic',
 'Slovenia',
 'Germany',
 'Portugal',
 'Hungary',
 'Colombia',
 'New Zealand',
 'Norway',
 'Latvia']

#Countries to highlight
Desired=['United States','Mexico','Japan','China','Korea,Rep.','Ireland','France','Italy']

np.random.seed(0)
Value=np.random.rand(len(Countries))
df = pd.DataFrame({'Countries': Countries,'Value': Value,})
df.sort_values(['Value'],inplace=True)

df.set_index('Countries',drop=True,inplace=True)
ax_1 = df['Value'].plot(kind='bar', title ="graph", figsize=(10, 6), fontsize=12)
ax_1.set_xlabel("Country Name", fontsize=12)



for ticks in ax_1.xaxis.get_major_ticks():
    if ticks.label1.get_text() not in Desired:
        ticks.label1.set_visible(False)
        ax_1.patches[df.index.get_indexer([ticks.label1.get_text()])[0]].set_facecolor('w')
        ax_1.patches[df.index.get_indexer([ticks.label1.get_text()])[0]].set_edgecolor('black')
    else:
        ax_1.patches[df.index.get_indexer([ticks.label1.get_text()])[0]].set_facecolor('r')
        

enter image description here

关于python - 仅具有高亮值的 matplotlib 条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53716855/

相关文章:

python - 用于侧面输入的高效 ParDo 设置或 start_bundle

python - 读取 Dataframe 中的 2 个单元格值

python - 在 Matplotlib python 中绘制字典内的字典

python - 按正则表达式和标签组合的 Pandas 过滤器

python - 如何使数据帧列表的长度全部相等

python - 如何在 matplotlib 中注释 x 轴的范围?

python - Alphashape 和 PolygonPatch : basic example doesn't work. 为什么?

python - 从包含 RGB 图像数据的 1-dim 数组中检索 RGB 图像

python - Pandas GroupBy 值的频率

python - nginx - 从上游读取响应 header 时上游发送了太大的 header