【问题标题】:Is there a way to plot columns with the same name as the same colour?有没有办法绘制与相同颜色同名的列?
【发布时间】:2020-05-29 20:58:33
【问题描述】:

我正在尝试绘制一个包含许多同名列的数据集。我试图重用我之前制作的绘图程序,但问题是它以不同的颜色绘制每一列,而不是我希望 same name 的每一列都是 same颜色。此外,它将每一列添加到图例中,导致我附上的图中出现很多混乱。有没有办法只为每种颜色贴一个标签?任何解决这些问题的帮助将不胜感激!

Here is an example plot 如您所见,它没有将两个“O”绘制为一种颜色,而是将其绘制为两个,因为有两列数据,并在图例中创建了一个副本。

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

s = pd.read_csv('Edit_This.csv') # reads the .csv file can be found in dropbox > shared manuscripts > Ravi Ben MoOx Nucleation and LEIS > Figures

xmin = 0 # Change these bounds as needed
xmax = 1
ymin = 0
ymax = 2500

y_axis_columns = s.columns[1:] # Reads column data to plot y values from index 1 onwards

for col_name in y_axis_columns: # Loop to plot all columns until it finds an empty one
   plt.plot(s.depth,s[col_name], label=col_name)
   plt.tight_layout()
   from matplotlib.ticker import MaxNLocator
   axes = plt.gca()
   axes.yaxis.set_major_locator(MaxNLocator(integer=True))
   axes.set_xlim([xmin,xmax])
   axes.set_ylim(ymin,ymax)
   plt.xlabel('Depth (nm)', fontsize=12) # Edit as needed
   plt.ylabel('Intensity (counts/nC)', fontsize=12) 
   plt.legend(loc='upper center', fontsize=10) # Defines legend formatting
   plt.savefig("6_Sputter1_5-222cyc_SiOx.png", dpi = 1200) # Edit export file name and DPI here
plt.show()

你可以从这个link下载数据

【问题讨论】:

    标签: python pandas matplotlib data-processing


    【解决方案1】:

    好的,问题出现了,因为您有多个具有相同名称的列。所以,你需要处理它。下面的代码就是这样做的:

    import pandas as pd
    import matplotlib.pyplot as plt
    from matplotlib.lines import Line2D
    
    s = pd.read_csv('Edit_This.csv') # reads the .csv file can be found in dropbox > shared manuscripts > Ravi Ben MoOx Nucleation and LEIS > Figures
    
    xmin = 0 # Change these bounds as needed
    xmax = 1
    ymin = 0
    ymax = 2500
    
    y_axis_columns = s.columns[1:] # Reads column data to plot y values from index 1 onwards
    
    
    # this is the color dictionary
    colors = {"O": 'r', "Mo": 'b', 'Al':'g'}
    
    for col_name in y_axis_columns: # Loop to plot all columns until it finds an empty one
       plt.plot(s.depth,s[col_name], label=col_name, color=colors[col_name.split('.')[0]])
       plt.tight_layout()
       from matplotlib.ticker import MaxNLocator
       axes = plt.gca()
    
    axes.yaxis.set_major_locator(MaxNLocator(integer=True))
    axes.set_xlim([xmin,xmax])
    axes.set_ylim(ymin,ymax)
    plt.xlabel('Depth (nm)', fontsize=12) # Edit as needed
    plt.ylabel('Intensity (counts/nC)', fontsize=12) 
    
    # Defines legend formatting
    custom_lines = [Line2D([0], [0], color='r', lw=4),
                    Line2D([0], [0], color='b', lw=4),
                    Line2D([0], [0], color='g', lw=4)]
    plt.legend(custom_lines, ['O', 'Mo', 'Al'], loc='upper center', fontsize=10)
    
    plt.savefig("6_Sputter1_5-222cyc_SiOx.png", dpi = 1200) # Edit export file name and DPI here
    plt.show()
    

    生成以下图像:

    【讨论】:

    • 我猜最后一个问题,并不是说它的问题太大,但无论如何,是否有消除继续向代码添加 O.x(其中 x 是每个额外的 O 列)的需要?这只会产生一个更强大的解决方案,能够获取任何数据集并生成绘图。同样不是太紧迫的问题,只是好奇它是否可行。
    • 哇,只有几个字符。你介意解释一下“color=colors[col_name.split('.')[0]]”是如何解决这个问题的吗?
    • 没问题... col_name 可以是O, O.1, O.2, ...等等。如您所见,我们只对@987654328 之前的部分感兴趣@。所以,我们需要根据.进行拆分,然后选择第一部分。所以,"O.1".split('.')[0] 将只是 O
    猜你喜欢
    • 2020-09-09
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 2020-11-28
    相关资源
    最近更新 更多