我重新提出了问题。
我有一个熊猫数据框,如下所示:
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
,每条线都由相关性数据表示。答案 0 :(得分:0)
因为您更改了问题,所以这里是更新的答案:
查看代码中的注释
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)