UChicago-CCA-2021 / Frequently-Asked-Questions

Repository to ask questions - please use the issues page to ask your questions.
0 stars 0 forks source link

How to adjust the x-axis for neat plots? #22

Closed jinfei1125 closed 3 years ago

jinfei1125 commented 3 years ago

Hi, I draw the following plot in my week2 homework: 图片 I realized that it's impossible to find what words are in the x-axis. However, after some googling, I still don't know what's the best solution to this problem. Some google pages suggest I can use matplotlib ticker module, but that would omit a number of variables' name in x-axis, so I am hesitating to use it... This is what I found on google:

import matplotlib.ticker as ticker

def plot_rmse(train_months, forecast_days, ylim=(0,100), tick_spacing=2):
    s = "VAR_rmse_{}month_{}day.csv".format(train_months,forecast_days)
    data = pd.read_csv(s)
    fig, ax = plt.subplots(figsize = (20,10))
    plt.plot(data.date, data['node-046-d2w'].values, color = 'r')
    ax.set_title("VAR_rmse_{}month_{}day".format(train_months,forecast_days))
    ax.set_ylim(ylim)
    ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
    plt.xticks(rotation=90)
    plt.show()
bhargavvader commented 3 years ago

Yeah, it's going to be difficult for this plot to be neat... I think actually not having a few of the variable names omitted is ok, apart from making the x-axis really long I can't think of a satisfactory solution.

lilygrier commented 3 years ago

You can pass in a number to .plot(). So if you just wanted to show the first 30 entries, you could say reddit_cfdist[10].plot(30). That would of course only show the top n entries, so a tradeoff between complete information and readability

jinfei1125 commented 3 years ago

Thanks so much for your kind help @lilygrier @bhargavvader!