【问题标题】:how to specified color in plot from matplotlib如何从matplotlib中指定绘图颜色
【发布时间】:2020-05-31 10:33:50
【问题描述】:

我有来自 fifa 19 的球员数据。它有沉着、点球能力和国籍。我想绘制一个 X 轴是沉着,Y 轴是点球能力的图,但是如果玩家来自某个国籍,我想给它着色。我有这个代码:

import pandas
import math
import matplotlib.pyplot as plt

df= pandas.read_csv('fifa19.csv')

n= len(df)
x=[]
y=[]
fig, ax=plt.subplots()
j=0
for i in range(n):
   if(not math.isnan(df['Composure'][i])
       and not math.isnan(df['Penalties'][i])):
       x.append(df['Composure'][i])
       y.append(df['Penalties'][i])
   if(df['Nationality'][i]=='Brazil'): #it supposed to be blue if the nationality is brazil else is default(correct me if i'm wrong)
       ax.plot(x, y, 'o', color='blue')

ax.set_xlabel('Composure attribute')
ax.set_ylabel('Penalties attribute')
fig.show()

But it all turned to blue. 我试图改变这样的循环:

if(df['Nationality'][i]=='Brazil'): 
        ax.plot(x[i], y[i], 'o', color='blue')

但它显示“IndexError: list index out of range”。知道如何使用绘图对其进行着色吗?感谢您的帮助。

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    如果他们来自巴西,你只会画画吗?否则,您可以编写一个 else 语句并选择所需的颜色

    【讨论】:

    • 我试过了,但错误还是一样,IndexError: list index out of range
    【解决方案2】:

    使用此代码

    import matplotlib
    from matplotlib import style
    
    plt.title("Natitk")
    plt.plot([1,4,7],[3,4,5],linewidth=4, Label="first", color="yellow")
    plt.plot([5,4,7],[2,6,8],linewidth=4, Label="second")
    
    plt.legend()
    
    plt.xlabel("x")
    style.use("ggplot")
    plt.ylabel("y")
    

    将黄色存储在变量中,然后使用 if 语句

    【讨论】:

      猜你喜欢
      • 2018-08-12
      • 1970-01-01
      • 2022-01-20
      • 2013-12-24
      • 2013-07-19
      • 2019-03-23
      • 2015-02-03
      • 2023-04-04
      • 1970-01-01
      相关资源
      最近更新 更多