0

I am new from matplotlib. May I know how to use loop function to plot this chart? Also, how to add spacing to the x-axis?

The code like this:

import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.ticker as ticker
%matplotlib inline

plt.figure(figsize=(15,6))

ax1 = plt.subplot(2,5,1)
plt.plot(tickers_data.index.strftime("%d"),tickers_data['Volume','XPEV'])
plt.ticklabel_format(style='plain', axis='y')
plt.title('XPEV')

ax2 = plt.subplot(2,5,2)
plt.plot(tickers_data.index.strftime("%d"),tickers_data['Volume','M'])
plt.ticklabel_format(style='plain', axis='y')
plt.title('M')

ax3 = plt.subplot(2,5,3)
plt.plot(tickers_data.index.strftime("%d"),tickers_data['Volume','MLCO'])
plt.ticklabel_format(style='plain', axis='y')
plt.title('MLCO')

ax4 = plt.subplot(2,5,4)
plt.plot(tickers_data.index.strftime("%d"),tickers_data['Volume','VIPS'])
plt.ticklabel_format(style='plain', axis='y')
plt.title('VIPS')

ax5 = plt.subplot(2,5,5)
plt.plot(tickers_data.index.strftime("%d"),tickers_data['Volume','HD'])
plt.ticklabel_format(style='plain', axis='y')
plt.title('HD')

enter image description here

1
  • Did you try calling plt.tight_layout() at the end? Also note that plt.subplot(2,5,1) is the old interface to create subplots. Using fig, axs = plt.subplots(2, 5) is now the recommended way. See e.g. matplotlib.org/stable/gallery/subplots_axes_and_figures/… for an introduction. Commented Apr 30, 2021 at 23:52

2 Answers 2

1
fig, axs = plt.subplots(1,5)
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)
plt.tight_layout()
plt.show()

Here are some explanations:

  • plt.subplots(nrows, ncols) returns Fig object and Axes object (array of Axes if nrows * ncols > 1).
  • Basically you want to draw your plots on each Axes. Thus, you loop over the array of Axes and plot your data ax.plot().
  • When you have multiple Axes drawn and it looks ugly, use plt.tight_layout(). It adds padding between Axes, making your figures look neat.
Sign up to request clarification or add additional context in comments.

6 Comments

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
Hello, thank you for your help. After I read your answer, I tried to plot the second row of charts. I know I should use Nested loop, however, I still do not figure it out how can I do it. May you please help me again?
@janicewww sure, I've added some explanations. Let me know if you need more explanations. Also checkout this demo from matplotlib's documentation.
@Gusti Adli Thanks for your explanation. Yes, I hv read the documentation but I still unable to figure it out of how I can pass the value to second loop.
Hello, I figure it out now. Your help save my life. Thank you so much.
|
0
fig,((axs),(axs2),(axs3))  = plt.subplots(3,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)
    

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

1 Comment

Please add more details to your answer such that others can learn from it - or is this something you wanted to add to your question, as this did not resolve the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.