繁体   English   中英

如何使用 Python-IDE Jupiter Notebook 更改条形图中 1 个特定条形的颜色

[英]How do I change the colour of 1 specific bar in a bar chart using Python- IDE Jupiter Notebook

我的代码目前是这个 - 使用我创建的日期集!

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import os

    desired_width=420
    pd.set_option('display.width', desired_width)
    np.set_printoptions(linewidth=desired_width)
    pd.set_option('display.max_columns',6)

    Data = pd.read_csv("/Volumes/DYLAN USB/Excel/DataSheets/Data Passing Feb EPL 202021.csv",index_col=0)

打印(数据头(20))

    fig, ax = plt.subplots()
    Data = Data.sort_values('Total Pass Completion %', ascending=False)
    ax.bar(Data.Team, Data['Total Pass Completion %'])
    ax.set_xticklabels (Data.Team, rotation=90, horizontalalignment = 'right', fontsize = '12')

   ax.set_title ('League Ranking and Points- EPL 2020/21 February', fontsize =22)
   ax.set_ylabel ('Pass Completition %')
   ax.set_xlabel ('Team Name')

如果您希望人们轻松解决您的问题,您需要提供一些示例数据。 我在下面创建了一些数据。 您可以索引条形对象matplotlib.patches.Rectangle并将它们设置为您想要的颜色

import pandas as pd 
import matplotlib.pyplot as plt 

Data = pd.DataFrame({'Total Pass Completion %': [100, 30, 8, 4], 'Team': ['A', 'B', 'C', 'D']})

fig, ax = plt.subplots()
Data = Data.sort_values('Total Pass Completion %', ascending=False)
ax.bar(Data.Team, Data['Total Pass Completion %'])
ax.set_xticklabels (Data.Team, rotation=90, horizontalalignment = 'right', fontsize = '12')

ax.set_title ('League Ranking and Points- EPL 2020/21 February', fontsize =22)
ax.set_ylabel ('Pass Completition %')
ax.set_xlabel ('Team Name')


ax.get_children()[1].set_color('r')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM