2024-09-29 19:32:21 发布
网友
我有数据帧total_year,它包含三列(year,action,comedy)
total_year
year
action
comedy
我想在x轴上绘制year列,以及action&comedy都在y轴上
如何在y轴上绘制两列(action和comedy)
我的代码只在y轴上绘制一列
total_year[-15:].plot(x='year', y='action', figsize=(10,5), grid=True)
可以为函数的y参数提供多个列名。这些应该在list中指定,如下所示
y
list
df.plot(x="year", y=["action", "comedy"])
完整示例:
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()
Pandas.DataFrame.plot()默认情况下,使用索引绘制X轴,所有其他数值列将用作Y值
Pandas.DataFrame.plot()
X
Y
因此,将year列设置为索引将实现以下功能:
total_year.set_index('year').plot(figsize=(10,5), grid=True)
x
numeric
'date'
string
datetime
x=
import pandas as pd # test data data = {'year': [1914, 1915, 1916, 1919, 1920], 'action': [2.67, 3.43, 3.26, 2.82, 1.75], 'comedy': [2.53, 2.93, 3.02, 3.37, 3.45], 'test1': ['a', 'b', 'c', 'd', 'e'], 'date': ['1914-01-01', '1915-01-01', '1916-01-01', '1919-01-01', '1920-01-01']} # create the dataframe df = pd.DataFrame(data) # display(df) year action comedy test1 date 0 1914 2.67 2.53 a 1914-01-01 1 1915 3.43 2.93 b 1915-01-01 2 1916 3.26 3.02 c 1916-01-01 3 1919 2.82 3.37 d 1919-01-01 4 1920 1.75 3.45 e 1920-01-01 # plot the dataframe df.plot(x='year', figsize=(10, 5), grid=True)
可以为函数的
y
参数提供多个列名。这些应该在list
中指定,如下所示完整示例:
Pandas.DataFrame.plot()
默认情况下,使用索引绘制X
轴,所有其他数值列将用作Y
值因此,将
year
列设置为索引将实现以下功能:x
参数指定一列。numeric
值的其余列将用于y
李>'date'
保留为string
。但是,如果将'date'
转换为datetime
{'date'
列李>y
参数,如此answer中所示,但如果dataframe仅包含要打印的列,则仅指定x
参数李>x=
。相关问题 更多 >
编程相关推荐