简体   繁体   English

如何用python制作x y线图?

[英]How to make a x y line plot with python?

Hello I have the following dataframe: 您好我有以下数据帧:

df = [{'Column1': 1, 'Colunm2': 'A', 'Colunm3': 2}, 
      {'Column1': 2, 'Colunm2': 'A', 'Colunm3': 4},
      {'Column1': 3, 'Colunm2': 'A', 'Colunm3': 1},
      {'Column1': 1, 'Colunm2': 'B', 'Colunm3': 7},
      {'Column1': 2, 'Colunm2': 'B', 'Colunm3': 2},
      {'Column1': 3, 'Colunm2': 'B', 'Colunm3': 9}]

How can I make a XY plot between Column1 and Colunm3 but plotting two diffentent lines, one for values Colunm2 = 'A' and other for values Colunm2 ='B' ? 如何在Column1Colunm3之间绘制XY图,但绘制两条不同的线,一条用于值Colunm2 = 'A' ,另一条用于值Colunm2 ='B'

IIUC: IIUC:

import pandas as pd
df1 = pd.DataFrame(df)
fig,ax = plt.subplots()
for i,g in df1.groupby('Colunm2'):
    g.plot('Column1','Colunm3',ax=ax,label=i)

在此输入图像描述

As pointed out by @ScottBoston in his comment there is also sns.pointplot where the hue parameter is effectively a groupby. 正如@ScottBoston在他的评论中指出的那样,还有sns.pointplot ,其中hue参数实际上是一个groupby。

import sns.apiponly as sns
sns.pointplot('Column1', 'Colunm3', data=DataFrame(df), hue='Colunm2')
plt.ylabel('Colunm3')

在此输入图像描述

You can try this: 你可以试试这个:

import matplotlib.pyplot as plt

df = [{'Column1': 1, 'Colunm2': 'A', 'Colunm3': 2},
      {'Column1': 2, 'Colunm2': 'A', 'Colunm3': 4},
      {'Column1': 3, 'Colunm2': 'A', 'Colunm3': 1},
      {'Column1': 1, 'Colunm2': 'B', 'Colunm3': 7},
      {'Column1': 2, 'Colunm2': 'B', 'Colunm3': 2},
      {'Column1': 3, 'Colunm2': 'B', 'Colunm3': 9}]

first = [(i["Column1"], i['Colunm3'])  for i in df if i['Colunm2'] == 'A']
second = [(i["Column1"], i['Colunm3']) for i in df if i['Colunm2'] == 'B']


plt.plot([a for a, b in first], [b for a, b in first])
plt.plot([a for a, b in second], [b for a, b in second])

plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM