【发布时间】: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