Remove dashes from grid [duplicate] - python

This question already has an answer here:
Remove the x-axis ticks while keeping the grids (matplotlib) [duplicate]
(1 answer)
Closed 5 years ago.
If I run this code
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.xaxis.set_tick_params(length=0,labelsize=0)
ax.grid(True)
I get the following:
The xaxis ticks and labels don't show (as expected), but some dashes appear on the bottom of the plot (the first three of which I have circled in red).
How can I remove them? I have looked at the documentation for grids, but can't find anything.
An answer which teaches me how I could have figured out how to do this by looking at the documentation would be particularly useful.

What you see are the ticklabels, which have size 0. Even zero sized ticks appear as a single dot because of antigraining.
You probably want to set the label off completely
ax.xaxis.set_tick_params(length=0,labelbottom=False)
You find out about this by looking at the available arguments in the documentation.

Related

Why will my y value not show up on my graph? [duplicate]

This question already has answers here:
How to plot a single point in matplotlib
(2 answers)
Closed yesterday.
I actually cannot seem to figure out why my y value will not show up on the graph. This is a much simpler graph than ones that I have done in the past, yet I seem to be struggling with this. What could be the reason that the y value won't show up?
When I change the value of y, the limits of the graph change accordingly but the value is still missing.
replace ax.plot(0, y) with replace ax.plot(0, y, marker='o')
Since (0, 2.5) is an infinitely small point, you need to give it a marker so it's visible.

how to draw a rectangle over a matplotlib figure, also overlaying the axes [duplicate]

This question already has answers here:
How to draw rectangle outside of the plot frame in Matplotlib
(2 answers)
How to position a matplotlib patch outside of the axes range (so that it could be next to the title, or legend, or anywhere on the figure)
(2 answers)
Closed 5 months ago.
I would like to draw a rectangle over a matplotlib figure, in a way that allows overlaying the axes.
The answers I found online only allow drawing a rectange inside the axes, but not overlaying them.
EDIT: This answer allows drawing outside the plot frame. However it does not overlay the axis. Namely, the axis is kept visible. See the example where the axis is still visible behind the red rectangle
See an example below for what I wish to achieve (Code for the bar plot can be taken from here. For the example, the figure was edited with a simple paint software).
It is drawn via matplotlib.pyplot.hist() function in matplotlib

Matplotlib figure not working [duplicate]

This question already has answers here:
How do I change the size of figures drawn with Matplotlib?
(14 answers)
Closed 5 years ago.
I have all of my graphs working, so now I am trying to scale my plots down to a relatively small size (3x3in). I added the figure command and the graph goes blank. Right size, just blank. I'm obviously missing something basic but it's beating me. When I comment it out, it shows fine ( if a bit big). When I include it, it blanks the image.
What am I doing incorrectly ?
plt.setp(line[lindex], linewidth=1.0)
plt.setp(line[lindex+1], linewidth=2.0)
plt.xlabel("Months")
plt.ylabel("Score")
plt.title(CurrAppName + "- by month")
plt.legend()
# plt.figure(figsize=(3,3))
plt.show()
If this is your whole code, just put the plt.figure(figsize=(3, 3)) as the first line. Here, declaring plt.figure(...) in the end means you are creating a whole new plot after you've drawn into the last one. You don't want that.

How to change the default colors for multiple plots in matplotlib? [duplicate]

This question already has answers here:
How to set the default color cycle for all subplots with matplotlib?
(3 answers)
How to set default colormap in Matplotlib
(2 answers)
Closed 5 years ago.
I want to use the same colormap/color cycle/palette to every plot in a Jupyter notebook.
With the seaborn package, I can use:
seaborn.set_palette('Set1')
Is there a way to do the same using only matplotlib, without using seaborn?
I know how to define the colormap to each plot separately and I am aware of the predefined style (e.g, ggplot), but I can't find a way to define only the colormap to all the plots at once.
My intention is simplify the code for my students, thus using the intricate code behind set_palette() is not an option.
Edit: as the accepted answer shows, I was confusing colormap with color cycle.
The default colormap in matplotlib is "viridis". This is set at the rcParam "image.cmap".
The default colorcycle can be changed via the "axes.prop_cycle" rcParam.
import matplotlib.pyplot as plt
# to change default colormap
plt.rcParams["image.cmap"] = "Set1"
# to change default color cycle
plt.rcParams['axes.prop_cycle'] = plt.cycler(color=plt.cm.Set1.colors)

Align x-axis ticks in bar plot [duplicate]

This question already has an answer here:
Individually labeled bars for bar graphs in matplotlib / Python
(1 answer)
Closed 8 years ago.
I have a bar plot that looks like this:
How can I shift the x-axis labels so that they are centered under the corresponding bars? I'm trying to do this in a subplot.
You need to either use the set_xticks() function for your axis with a properly spaced number array (which you probably have in your code already, but not set properly), or you can use the ax.bar(x, y, align='center') command when setting up the bar chart. See the following answer:
Individually labeled bars for bar graphs in matplotlib / Python

Categories