【问题标题】:How to change color between two different columns in a scatterplot?如何更改散点图中两个不同列之间的颜色?
【发布时间】:2021-03-11 23:57:11
【问题描述】:

我有以下代码:

plt.scatter(moving_avg_temp.chicagoMA, moving_avg_temp.globalMA)
plt.title('Correlation between Chicago & Global 5 Year MA')
plt.xlabel('Chicago 5 Year MA')
plt.ylabel('Global 5 Year MA')
plt.show()

这会产生一个散点图,其中每个数据点都是相同的颜色。我试图让moving_avg_temp.chicagoMA 的颜色与moving_avg_temp.globalMA 不同,以可视化两个变量之间的相关性。

我正在使用 pandas 和 Matplotlib.pyplot。

【问题讨论】:

    标签: python-3.x pandas matplotlib scatter-plot


    【解决方案1】:

    这是一个可能对您有所帮助的代码 sn-p:

    import matplotlib.pyplot as plt
    %matplotlib inline
    
    
    x = [1,2,3,4,7,8,7]
    y = [4,1,3,6,3,6,8]
    
    plt.scatter(x, y, c='red')
    
    x2 = [5,6,7,8]
    y2 = [1,3,5,2]
    
    plt.scatter(x2, y2, c='lightblue')
    
    plt.title('Correlation between Chicago & Global 5 Year MA')
    plt.xlabel('Chicago 5 Year MA')
    plt.ylabel('Global 5 Year MA')
    
    plt.show()
    

    输出:

    这是 MatplotLib 中所有颜色名称的列表 --> https://matplotlib.org/examples/color/named_colors.html

    【讨论】:

      猜你喜欢
      • 2010-11-26
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多