Home > Software engineering >  adjusting graph in maplotlib (python)
adjusting graph in maplotlib (python)

Time:10-07

graph

how do I make this graph infill all the square around it? (I colored the part that I want to take off in yellow, for reference)

CodePudding user response:

Try setting the min and max of your x and y axes.

CodePudding user response:

Normally I use two methods to adjust axis limits depending on a situation.

When a graph is simple, axis.set_ylim(bottom, top) method is a quick way to directly change y-axis (you might know this already).

Another way is to use matplotlib.ticker. It gives you more utilities to adjust axis ticks in your graph. https://matplotlib.org/3.1.1/gallery/ticks_and_spines/tick-formatters.html

I'm guessing you're using a list of strings to set yaxis tick labels. You may want to set locations (float numbers) and labels (string) of y-axis ticks separatedly. Then set the limits on locations like the following snippet.

import matplotlib.pyplot as plt
import matplotlib.ticker as mt

fig, ax = plt.subplots(1,1)
ax.plot([0,1,2], [0,1,2])
ax.yaxis.set_major_locator(mt.FixedLocator([0,1,2]))
ax.yaxis.set_major_formatter(mt.FixedFormatter(["String1", "String2", "String3"]))
ax.set_ylim(bottom=0, top=2)

It gives you this: generated figure

  • Related