Twitch Project (Python Visualisation) - How do I re-scale the x and y axes on a line graph using matplot lib?

In the 2nd part of the Twitch Project we are asked to make a line graph using matplotlib.

When Mike plots ‘viewers_hour vs hours’ his x axis is scaled differently to his y axis like so:

Whereas when I post the exact same code my x and y axes are scaled in a weird way.

How can I get my graph to look the same as his? How do I re-scale my x axis so that the hours are stretched out?

I tried to set the boundaries for each axis like so:

plt.axis([0, 23, 0, 120])

but it didn’t work out. I could move on but I’d love to know why this happens and how I can fix it in the future!

Thank you.

It’s hard to be certain without observing more of your code. In the future check this Code_Formatting_FAQ for info on posting formatted code to the forum. It helps immensely when diagnosing problems.

I think the error occurs when you call plt.subplot()... on line 76. Technically that’s supposed to create a new subplot (it used to be suitable for accessing an already created axis instance but that is listed for deprecation so try to avoid it).

I’m not certain how this would affect an existing plot with the .set_xticks() method but I can see it being problematic. It does however look like you’ve wound up with more than one plot/axis. Without some borders I can’t quite tell if it’s a problem with the x limits or if the axis itself is the wrong size.

Personally I’d always advocate for using axis references to update an existing axis. e.g.

fig, ax = plt.subplots(1, 1)
ax. plot(somestuff)
ax.set_xticks((0, 1, 2, 3, 4, 5))  # using the axis reference: ax

Technically something like ax.autoscale_view() might also work (if you’ve not manually fiddled with the limits).