Bil*_*utt 8 python plot matplotlib pandas data-science
我有数据帧total_year,其中包含三列(年,动作,喜剧).
total_year
我想在X轴上绘制年份列,在Y轴上绘制(动作和喜剧).
我如何在Y轴上绘制两列(aciton和喜剧).这是我的代码.它在Y轴上仅绘制1列.
total_year[-15:].plot(x='year', y='action' ,figsize=(10,5), grid=True )
Run Code Online (Sandbox Code Playgroud)
Imp*_*est 18
您可以提供多个列来绘制ypandas绘图函数的参数,而不是单个列名称.那些应该被指定为列表.即:
df.plot(x="year", y=["action", "comedy"])
Run Code Online (Sandbox Code Playgroud)
完整的例子:
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()
Run Code Online (Sandbox Code Playgroud)
Pandas.DataFrame.plot()默认情况下,使用索引绘制X轴,所有其他数字列将用作Y值.
因此将year列设置为索引将起到作用:
total_year.set_index('year').plot(figsize=(10,5), grid=True)
Run Code Online (Sandbox Code Playgroud)
pandas.DataFrame.plot,只需为x参数指定一列即可。
numeric值的列将用于y。'date'保留为string. 但是,如果'date'转换为 a datetime dtype,绘图 API 还将'date'在 y 轴上绘制列。y中所示指定参数,但如果数据帧仅包含要绘制的列,则仅指定参数。xx=。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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18370 次 |
| 最近记录: |