【发布时间】:2021-01-13 21:02:31
【问题描述】:
我试图在一个图形上放置四个子图。 我想要的东西是:
1- 该图引入了自己的 x 和 y 标签,我不希望这样。
2- 我想知道是否可以在子图的所有标签中为 y 轴标签设置相似的值
3- 我想要的实际数字可能包含 3x3 大的子图(最多 9 个子图)。有没有办法制作某种函数,可以从每个子图的数据框中提取数据并绘制图表?
这是我使用的代码和输出图。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
fig, (df_256,df_128,df_64,df_32) = plt.subplots(4, 2, sharex='col', sharey='row')
file_locn = ''r'C:\Users\me\Desktop\output.xlsx'''
df = pd.read_excel(file_locn, sheet_name='1', header=[0,1])
#print(df)
df_256 = df.xs(256, axis=1, level=0)
df_128 = df.xs(128, axis=1, level=0)
df_64 = df.xs(64, axis=1, level=0)
df_32 = df.xs(32, axis=1, level=0)
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax1.set_xscale('symlog', base=2)
ax2.set_xscale('symlog', base=2)
ax3.set_xscale('symlog', base=2)
ax4.set_xscale('symlog', base=2)
ax1.set_yscale('log')
ax2.set_yscale('log')
ax3.set_yscale('log')
ax4.set_yscale('log')
'''print(df_256)
print(df_128)
print(df_64)
print(df_32)'''
color = ['blue', 'limegreen', '#bc15b0', 'indigo']
linestyle = ["-", ":", "--", "-."]
plot_lines = ["A", "B", "C", "D"]
df_256.set_index('X').plot( style=linestyle,ax=ax1)
df_128.set_index('X').plot(style=linestyle,ax=ax2)
df_64.set_index('X').plot( style=linestyle,ax=ax3)
df_32.set_index('X').plot( style=linestyle,ax=ax4)
plt.show()
【问题讨论】:
-
您使用从未使用过的
plt.subplots()命令创建 8 个 Axes 对象,然后使用 4 个ax1=fig.add_subplot()...命令创建另外 4 个。将第一个subplots()命令更改为fig=plt.figure(),你应该没问题(或者,不要调用由plt.subplots()df_128等创建的Axes 对象,并立即覆盖下面几行中的这些句柄,而是调用它们是明智的ax1, ax2, ...并使用下面的那些而不需要调用add_subplot4 次)。 -
@tmdavison 我对python和pandas很陌生,对这些东西没有具体的了解。正如你所说,我改变了第一个'subplots()',但它给了我一个错误。 'TypeError: __init__() 得到了一个意外的关键字参数 'sharex''
标签: python pandas matplotlib subplot figure