pandas中matlplotlib折线图设置大小
时间: 2024-01-17 13:04:45 浏览: 20
使用pandas自带的plot方法绘制折线图时,可以通过设置figsize参数来控制图像大小,示例代码如下:
```
import pandas as pd
import matplotlib.pyplot as plt
# 生成数据
df = pd.DataFrame({'x': [1, 2, 3, 4, 5], 'y': [2, 4, 6, 8, 10]})
# 绘制折线图
fig, ax = plt.subplots(figsize=(8, 6)) # 设置图像大小
df.plot(x='x', y='y', ax=ax)
plt.show()
```
在上面的代码中,通过subplots()方法创建一个名为fig的图像对象,并设置宽度为8英寸,高度为6英寸。然后,通过plot()方法在图像对象上创建一个名为ax的坐标系对象,并使用pandas的DataFrame对象df绘制折线图。最后,通过show()方法显示图像。
相关问题
pandas中matlplotlib折线图设置尺寸
好的,关于pandas中matlplotlib折线图设置尺寸,可以通过调用matplotlib.pyplot的figure方法来设置图形的大小,例如:
```
import pandas as pd
import matplotlib.pyplot as plt
data = {'year':[2010,2011,2012,2013,2014,2015],
'sales':[2.5,2.7,3.0,3.2,3.5,3.9]}
df = pd.DataFrame(data)
plt.figure(figsize=(8,6))
plt.plot(df['year'], df['sales'], color='green')
plt.title('Sales over year')
plt.xlabel('Year')
plt.ylabel('Sales')
plt.show()
```
在这个例子中,我们通过`plt.figure(figsize=(8,6))`方法设置图形的大小为宽度为8,高度为6。你可以根据自己的需要调整图形的大小。
pandas中设置matlplotlib折线图的大小
您可以使用matplotlib中的figsize参数来设置折线图的大小。例如,您可以使用以下代码来创建一个大小为10x8的折线图:
```
import pandas as pd
import matplotlib.pyplot as plt
# create a sample dataframe
data = {'year': [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019],
'sales': [100, 125, 150, 175, 200, 225, 250, 275, 300, 325]}
df = pd.DataFrame(data)
# plot the data
plt.plot(df['year'], df['sales'])
# set the figure size
plt.figure(figsize=(10, 8))
# show the plot
plt.show()
```
请注意,设置图形大小的代码必须在绘制图形之前运行。