首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Matplotlib优化绘制图表的脚本

使用Matplotlib优化绘制图表的脚本
EN

Stack Overflow用户
提问于 2021-05-01 09:30:25
回答 1查看 42关注 0票数 0

我想绘制一个dataframe,它在这个dataframe中有41个列,所以有41个图表要绘制。我写了一个脚本,但是它加载得太慢了。是否有优化此脚本的解决方案?是否可以使用循环函数来简化zip函数中的列表?

代码语言:javascript
复制
import matplotlib.pyplot as plt
import pandas as pd

fig,((axs),(axs2),(axs3),(axs4),(axs5),(axs6),(axs7),(axs8),(axs9)) = plt.subplots(9,5,figsize=(15,6))

for ax, y in zip(axs,['XPEV','M','MLCO','VIPS','HD']):
    ax.plot(tickers_data.index.strftime("%d"),tickers_data['Volume',y])
    ax.ticklabel_format(style='plain', axis='y')
    ax.set_title(y)
    
    for ax, y in zip(axs2,['LVS','PTON','SBUX','BLMN','NCLH']):
        ax.plot(tickers_data.index.strftime("%d"),tickers_data['Volume',y])
        ax.ticklabel_format(style='plain', axis='y')
        ax.set_title(y)
        
        for ax, y in zip(axs3,['NIO','NKE','NKLA','NLS','QS']):
            ax.plot(tickers_data.index.strftime("%d"),tickers_data['Volume',y])
            ax.ticklabel_format(style='plain', axis='y')
            ax.set_title(y)
            
            for ax, y in zip(axs4,['AYRO','RMO','TSLA','XL','ASO']):
                ax.plot(tickers_data.index.strftime("%d"),tickers_data['Volume',y])
                ax.ticklabel_format(style='plain', axis='y')
                ax.set_title(y)
                
                for ax, y in zip(axs5,['TOL','VSTO','BABA','FTCH','RIDE']):
                    ax.plot(tickers_data.index.strftime("%d"),tickers_data['Volume',y])
                    ax.ticklabel_format(style='plain', axis='y')
                    ax.set_title(y)

                    for ax, y in zip(axs6,['EBAY','DS','DKNG','DHI','UAA']):
                        ax.plot(tickers_data.index.strftime("%d"),tickers_data['Volume',y])
                        ax.ticklabel_format(style='plain', axis='y')
                        ax.set_title(y)
                        
                        for ax, y in zip(axs7,['VFC','TPX','ARVL','GM','GOEV']):
                            ax.plot(tickers_data.index.strftime("%d"),tickers_data['Volume',y])
                            ax.ticklabel_format(style='plain', axis='y')
                            ax.set_title(y)
                            
                            for ax, y in zip(axs8,['PLBY','CCL','GME','CVNA','LOTZ']):
                                ax.plot(tickers_data.index.strftime("%d"),tickers_data['Volume',y])
                                ax.ticklabel_format(style='plain', axis='y')
                                ax.set_title(y)
                                
                                for ax, y in zip(axs9,['F']):
                                    ax.plot(tickers_data.index.strftime("%d"),tickers_data['Volume',y])
                                    ax.ticklabel_format(style='plain', axis='y')
                                    ax.set_title(y)
                                

plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
plt.show()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-01 18:18:23

没有样本数据,所以我从简化steps

  • tight_layout()返回的plt.subplots()到1D

  • 的轴表开始,在列中,1级索引H 29<代码>H 110简单<

>D11>,所以我已经评论了H 215F 216/code>。

代码语言:javascript
复制
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

tickers_data = pd.DataFrame({("Volume",t):np.random.randint(20,200, 7) for t in ['XPEV','M','MLCO','VIPS','HD']+['LVS','PTON','SBUX','BLMN','NCLH']+['NIO','NKE','NKLA','NLS','QS']+['AYRO','RMO','TSLA','XL','ASO']+['TOL','VSTO','BABA','FTCH','RIDE']+['EBAY','DS','DKNG','DHI','UAA']+
                            ['VFC','TPX','ARVL','GM','GOEV']+['PLBY','CCL','GME','CVNA','LOTZ']+['F']}, index=pd.date_range("1-Jan-2021", periods=7))

tickers_data
fig,ax = plt.subplots(9,5,figsize=(15,6))

ax = np.array(ax).flatten()
for i,y in enumerate(tickers_data.columns.get_level_values(1)):
    ax[i].plot(tickers_data.index.strftime("%d"),tickers_data['Volume',y])
    ax[i].ticklabel_format(style='plain', axis='y')
    ax[i].set_title(y)
    
# plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
plt.show()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67344726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档