Python 如何在折线图的 Y 轴上绘制两列单个 DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47775220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to plot two columns of single DataFrame on Y axis of linegraph
提问by Bilal Butt
I have dataframe total_year which contains three columns(year,action,comedy) .
我有包含三列 (year,action,comedy) 的数据框 total_year 。
total_year
总计_年
i want to plot year column on X axis and (action & comedy) both on Y axis.
我想在 X 轴上绘制年份列,在 Y 轴上绘制(动作和喜剧)列。
How i can plot two columns(aciton and comedy) on Y axis. Here is my code. it plot only 1 column on Y axis.
我如何在 Y 轴上绘制两列(aciton 和 comedy)。这是我的代码。它仅在 Y 轴上绘制 1 列。
total_year[-15:].plot(x='year', y='action' ,figsize=(10,5), grid=True )
采纳答案by MaxU
Pandas.DataFrame.plot()
per default uses index for plotting X
axis, all other numericcolumns will be used as Y
values.
Pandas.DataFrame.plot()
默认情况下使用索引绘制X
轴,所有其他数字列将用作Y
值。
So setting year
column as index will do the trick:
因此,将year
列设置为索引即可解决问题:
total_year.set_index('year').plot(figsize=(10,5), grid=True)
回答by ImportanceOfBeingErnest
Instead of a single column name you may provide several columns to plot to the y
argument of pandas plotting function. Those should be specified as list. I.e.:
您可以提供多列来绘制y
熊猫绘图函数的参数,而不是单个列名。那些应该被指定为列表。IE:
df.plot(x="year", y=["action", "comedy"])
Complete example:
完整示例:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({"year": [1914,1915,1916,1919,1920],
"action" : [2.6,3.4,3.25,2.8,1.75],
"comedy" : [2.5,2.9,3.0,3.3,3.4] })
df.plot(x="year", y=["action", "comedy"])
plt.show()
回答by Ayyasamy
All the independent/feature variable can be plotted with deendent(outcome-Y ) variable using the below mentioned loop and code based on the type of feature variable, whether that is object/int64/float64.
所有独立/特征变量都可以使用 deendent(outcome-Y) 变量绘制,使用下面提到的循环和基于特征变量类型的代码,无论是 object/int64/float64。
In this case Feature_col_X1 ( contains feature list ) and Target_col_Y1 is the target one, I am passing this in a defined function and I can get all the plot for 20 features to the 1 out
在这种情况下,Feature_col_X1(包含特征列表)和 Target_col_Y1 是目标,我将它传递到一个定义的函数中,我可以将 20 个特征的所有绘图输出到 1
def plotforallvariables(Feature_col_X1,Target_col_Y1):
for i in range(len(Feature_col_X1)):
idx=Feature_col_X1[i]
try:
if data[idx].dtype =='O':
#print('categorical')
#%matplotlib inline
#print(idx,'in X axis and also',Target_col_Y1 ,'in Y axis')
pd.crosstab(data[idx],data[Target_col_Y1]).plot(kind='bar')
#x=r'idx,'in X axis and also',Target_col_Y1 ,'in Y axis'
#plt.title('x')
#print(data[idx])
#print(data[Target_col_Y1])
#plt.xlabel(data[idx])
#plt.ylabel(data[Target_col_Y1])
elif data[idx].dtype =='int64':
#ax = plt.gca()
#data.plot(kind='line',x=data[idx],y=data[Target_col_Y1])
pd.crosstab(data[idx],data[Target_col_Y1]).plot(kind='line')
#data.plot.scatter(x=data[idx],y=data[Target_col_Y1])
#plt.show()
#print('integer')
elif data[idx].dtype =='float64':
#print('float')
pd.crosstab(data[idx],data[Target_col_Y1]).plot(kind='line')
#data.plot(kind='line',x=data[idx],y=data[Target_col_Y1])
except (ValueError,KeyError):
print('skip error')
plotforallvariables(Feature_col_X,Target_col_Y)