我重新表述了我的问题。
我有一个如下所示的 Pandas 数据框:
data = [[0, 'cat1', 1, 11], [33, 'cat1', 3, 52], [0, 'cat1', 4, 14], [11, 'cat2', 3, 22], [14, 'cat2', 2, 18], [0, 'cat2', 5, 13]]
df = pd.DataFrame.from_records(data_str, index=['time1', 'time2', 'time2', 'time3', 'time1', 'time1'], columns = ['Text Time', 'Tag', 'Relevance', 'Text length'])
df = df.sort_index()
df.plot(x='Text Time', y = 'Relevance')
Text Time Tag Relevance Text length
time1 0 cat1 1 11
time1 33 cat1 3 52
time2 85 cat1 4 14
time1 11 cat2 3 22
time2 99 cat2 2 18
time3 117 cat2 5 13
该Text Time
列是我的时间,X轴和相关性是y轴。现在我想:
Text Time
使用索引 ( time1
, time2
, time3
)分组cat1
并且cat2
,每条线都由相关性数据表示。因为你在这里改变了问题是更新的答案:
查看代码中的注释
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
%matplotlib inline
# read your dataframe and sort
df = pd.read_clipboard()
df.drop(columns=['length'], inplace=True)
df.rename(columns={'Text.1': 'Text length'}, inplace=True)
df.sort_values(['Text', 'Tag', 'Time'], inplace=True)
x = list(df['Time']) # set x axis by creating a list of time
fig, ax = plt.subplots() # plot mulitple lines
for xlabels, group in df.groupby(['Tag']): # group by Tag
df['Time'] = df['Time'].astype(str) # change time to a string to create xticks
xticks = list(df['Time']+'\n'+df['Text']+'\n'+df['Tag']) # create xticks
group.plot(kind='line',x='Time', y='Relevance', ax=ax)
ax.legend(['Cat1', 'Cat2'])
ax.set_xlabel('Time')
plt.xticks(x, xticks)
# resize plot
pos1 = ax.get_position()
pos2 = [pos1.x0, pos1.y0, pos1.width + 1, pos1.height + .5]
ax.set_position(pos2)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句