None of the above? Find out where to ask other questions
here
!
So, I’ve been working on recreating the bar chart with error and I’m able to get through a good chunk of it, but I’m having a problem with the code. Once I run the plt.axis code, my bars get scrunched up into the upper right hand corner of the chart and the dates get squished into the lower left hand corner. Same thing happens when if I comment out that line and run the ax.set_xticks or ax.set_ticklabels on the code. When I run it in python command line, I have the same problem, but I’m able to zoom to see the bars (but still can’t see the dates). Any thoughts on why this might be happening?
import codecademylib
from matplotlib import pyplot as plt
that makes sense if we’re referring to it in that way with the axis adjusting, thank you!
Alternatively, could we adjust the axis differently? what does that even do, give the chart more space to the left and right? because as soon as I do it all my data disappears if I don’t use the len as you suggested.
When I tried to replicate the exercise in jupyter, even I get the same result I do not understand the following warning message:
Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
if name == ‘main’:
If i put ‘years’ in plt.bar() i get directly what i get when i do :
Create an ax object using plt.subplot() . Use ax to set the x-axis ticks to be range(len(years)) and the x-axis labels to be the years list.
so why use 'years ’
can u help me out with this?
the following code allows me to move on to the next exercise, but the graph figure is empty. I see the title, the x and y labels and correct tick labels. Yet the bars do not show up at all.
What am I missing?
import codecademylib
from matplotlib import pyplot as plt
plt.figure(figsize=(10,8)) # Create a figure with height 8 and width 10
plt.bar(years,past_years_averages, yerr=error, capsize=5) # Plot bars in past_years_average, with error bar, and capsize 5
plt.axis([-0.5, 6.5, 70, 95]) # set axis to -0.5 to 6 on x-axis, and 70-95 on y-axis
ax = plt.subplot() # Create an object, use ax to set x-axis ticks and x-axis labels
ax.set_xticks(range(len(years)))
ax.set_xticklabels(years)
ax.set_title(‘Final Exam Averages’) # title the graph
ax.set_xlabel(‘Year’) # set xlabel to ‘Year’
ax.set_ylabel(‘Test average’) # set ylabel to ‘Test average’
plt.savefig(‘my_bar_chart.png’) # save figure
plt.show()
As for why you don’t see anything- What happens when you adjust your axis limits with plt.axis()? What were the original limits? Is your data in the new range?
I’d also be very careful about calling subplot after the axis has already been created. In fact, having just copy/pasted this code quickly it throws out a warning that in the future this will create an entirely new axis instance (I’m kinda surprised this wasn’t already the case tbh).
To ensure your code is still supported in future versions of matplotlib (and IMO just generally for a more correct way) define the axis first, then plot and then start fiddling with the values.
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot()
ax.bar(years,past_years_averages, yerr=error, capsize=5)
ax.axis([-0.5, 6.5, 70, 95]) # This is still an issue...
# Then start chaging ticks etc.
... etc.
I’m not certain that you need to go about chaging x-axis ticks and labels in this isntance but plot it and find out.
Thank you tgrtim. I think it had to do with the way I was setting the plt.axis line. Learning this stuff is certainly challenging and I need to be more methodical in my debugging.
From what I can find online, there does not appear to be a set function for matplotlib. Set is a function for python in general, but for a much different purpose than you’re looking for. There is a pet.setp() function, which might be what you’re thinking of. However, from what I can tell you can only pass in parameters that you would pass into the plot/bar/pie/hist function, such as marker, linestyle, etc…
Hello! I am confused in which situations do we need to use range(len(data_list)) and when just data_list.
I have bumped into the same error as other students here writing a code plt.bar(years, past_years_averages, yerr=error, capsize=5), when it should be as below:
I understand that without range(len(years)) everything would be messed up at the line [plt.axis([-0.5, 6.5, 70, 95])].
Would it be safe to consider that with plt.bar we always want to use range(len(dataset)) and we don’t need to do the same thing with y axis values?