在Jupyter Notebook中,可以使用matplotlib
库来绘制图形,并使用pandas
库来处理数据帧。要在绘图旁边显示数据帧,可以使用matplotlib
的subplot
功能来实现。
下面是实现的步骤:
matplotlib
和pandas
库。如果没有安装,可以使用以下命令进行安装:!pip install matplotlib pandas
import matplotlib.pyplot as plt
import pandas as pd
pandas
库的DataFrame
函数。假设数据帧的变量名为df
:df = pd.DataFrame({'x': [1, 2, 3, 4, 5], 'y': [10, 20, 30, 40, 50]})
matplotlib
的subplots
函数来创建图形和坐标轴:fig, ax = plt.subplots()
plot
函数。假设要绘制散点图,可以使用scatter
函数:ax.scatter(df['x'], df['y'])
matplotlib
的text
函数。将数据帧的字符串表示作为参数传递给text
函数,并指定显示的位置:ax.text(1.1, 0.5, str(df))
show
函数显示图形和数据帧:plt.show()
完整的代码示例如下:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({'x': [1, 2, 3, 4, 5], 'y': [10, 20, 30, 40, 50]})
fig, ax = plt.subplots()
ax.scatter(df['x'], df['y'])
ax.text(1.1, 0.5, str(df))
plt.show()
这样,就可以在Jupyter Notebook中的绘图旁边显示数据帧了。
领取专属 10元无门槛券
手把手带您无忧上云