This question already has answers here:
How to set the y-axis limit
(8 answers)
Closed 2 years ago.
I am drawing graph in tkinter using matplotlib. Here is code:
data = [[left[0],right[0]]]
X = ["left", "right"]
fig = Figure(figsize=(6, 4), dpi=96)
ax = fig.add_subplot(111)
barlist = ax.bar(X , data[0], color = 'b', width = 0.25)
barlist[0].set_color('r')
graph = FigureCanvasTkAgg(fig, master=win)
canvas = graph.get_tk_widget()
canvas.place(x= 150, y = 5)
How do I set y axis limit (maximum value)?
You could do:
ax.set_ylim(lower_limit,upper_limit)
Related
This question already has answers here:
Plot a horizontal line on a given plot
(7 answers)
Closed 1 year ago.
I would like to draw a horizontal line with matplotlib's plt.axhline() function, but I want the horizontal line to stop at the absolute value of 5 on the x-axis. How do I set xmax in plt.axhline() to stop at 5?
plt.figure()
plt.plot(np.arange(-60, 60, 20), np.arange(0, 1.2, 0.2))
plt.axhline(y = 0.5, xmax = 5, c= 'r')
You need to use plt.hlines instead, also specify a xmin and change c to color .
import matplotlib.pyplot as plt
import numpy as np
xmin = -65
plt.figure()
plt.plot(np.arange(-60, 60, 20), np.arange(0, 1.2, 0.2))
plt.hlines(y = 0.5, xmin=xmin , xmax = 5, color= 'r')
plt.xlim(left=xmin);
This question already has answers here:
How to draw vertical lines on a given plot
(6 answers)
Closed 1 year ago.
I'm using the following:
fig, ax = plt.subplots(figsize=(20, 10))
ax.set_ylim(bottom=0, top=10)
for i in range(4):
ax.axvline(x=i, ymin=5, ymax=9, color="red", linewidth=40)
Which gives:
I would expect there to be a vertical line at each point from y = 5 to y = 9.
You should use matplotlib.pyplot.vlines, as suggested by BigBen in the comment:
for i in range(4):
ax.vlines(x=i, ymin=5, ymax=9, color="red", linewidth=40)
If you look at the parameters for axvline, you see that ymin and ymax goes from 0 to 1. A fraction of your complete ylimit.
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.axvline.html
So you need something like .5 to .9 or calculate the appropriate fractions.
fig, ax = plt.subplots(figsize=(20, 10))
ax.set_ylim(bottom=0, top=10)
for i in range(4):
ax.axvline(x=i, ymin=.5, ymax=.9, color="red", linewidth=40)
Output:
This question already has answers here:
How to force the Y axis to only use integers
(3 answers)
Closed 8 months ago.
I am making a bot which tracks discord server stats and makes a graph of them.
While making the bot, I faced a problem. The bot shows floating point numbers in the graph which are not supposed to be there.
Is it possible to disable the float numbers and show only 12, 13, 14 instead of 12, 12.25, 12.50, etc?
Answer
I suppose your data are in a y list. In this case you can use ax.set_yticks() as here:
yticks = range(min(y), max(y) + 1)
ax.set_yticks(yticks)
Code
import matplotlib.pyplot as plt
plt.style.use('dark_background')
x = ['14.09', '15.09', '16.09', '17.09', '18.09']
y = [12, 13, 13, 14, 14]
fig, ax = plt.subplots()
ax.plot(x, y, color = 'green', linestyle = '-', marker = 'o', markerfacecolor = 'red')
ax.set_facecolor('white')
ax.set_ylabel('Member count')
ax.set_title("Member count for 'СПГ'")
plt.setp(ax.xaxis.get_majorticklabels(), rotation = 90)
yticks = range(min(y), max(y) + 1)
ax.set_yticks(yticks)
plt.show()
Output
This question already has answers here:
Aligning rotated xticklabels with their respective xticks
(5 answers)
Closed 4 months ago.
I am making a bar chart and I want to move the x-axis tick labels one position to left. Here is the code of the plot:
matplotlib.rcParams.update(matplotlib.rcParamsDefault)
plt.style.use(['seaborn-white', 'bmh'])
fig1, ax = plt.subplots()
palette = ['#2a5495', '#07a64c', '#e979ad', '#d88432', '#2a5495',
'#b7040e', '#82c5db', '#b9c09b', '#cd065d', '#4b117f']
x = np.array(df.index)
y = np.array(df.loc[:, 2015])
width = 1.0
lefts = [x * width for x, _ in enumerate(y)]
ax.bar(left = lefts, height = y, width = width, tick_label = x, color = palette, label = ranked_univs)
ax.axis(ymin = 0, ymax = 200, xmin = -0.5, xmax = 9.5)
ax.tick_params(axis='x', which='major', labelsize=8)
ax.set_xticklabels(ax.xaxis.get_majorticklabels(), rotation=45)
fig1.tight_layout()
plt.show()
And here is the bar chart:
Any clue?
Your labels are correctly positioned, as shown by the fact that if you were to rotate them 90°, they would be perfectly aligned with your bars.
fig1, ax = plt.subplots()
palette = ['#2a5495', '#07a64c', '#e979ad', '#d88432', '#2a5495',
'#b7040e', '#82c5db', '#b9c09b', '#cd065d', '#4b117f']
labels = ['Long misaligned label {}'.format(i) for i in range(10)]
x = range(10)
y = 100+100*np.random.random((10,))
width = 1.0
lefts = [x * width for x, _ in enumerate(y)]
ax.bar(left = lefts, height = y, width = width, tick_label = labels, color = palette)
ax.axis(ymin = 0, ymax = 200, xmin = -0.5, xmax = 9.5)
ax.tick_params(axis='x', which='major', labelsize=8)
ax.set_xticklabels(ax.xaxis.get_majorticklabels(), rotation=90)
fig1.tight_layout()
plt.show()
The problem is that the labels are centered horizontally, so when you rotate them 45°, they appear to be aligned with the wrong bar. To fix this, align the labels to the right, and they'll get back to their correct (visual) position.
plt.setp(ax.xaxis.get_majorticklabels(), ha='right')
Another (maybe simpler) option is to use the helper function Figure.autofmt_xdate(), which handles all of this for you.
See this question: How can I rotate xticklabels in matplotlib so that the spacing between each xticklabel is equal?
There the solution is to align the labels to their right side:
ax.set_xticklabels(xticklabels, rotation = 45, ha="right")
This question already has answers here:
change matplotlib axis settings
(2 answers)
Closed 7 years ago.
I have a graph where the yaxis ranges from -3 to 3. Does anyone know how I can modify the xticks or their corresponding xtick labels so they show at the y=0 level?
Thanks
ind = np.arange(len(labels))
width = 0.4
width2 = 0.73
width3 = .85
fig, ax = plt.subplots(figsize=(12,5), facecolor='white')
application = ax.bar(ind, values3, width3, color = 'g', alpha = .25)
admit = ax.bar(ind+((width3-width2)/2), values2, width2, color = '#537DDE', alpha = .6)
matric = ax.bar(ind+((width3-width)/2), values, width, color = '#E13F2A', alpha = .9)
ax.set_axis_bgcolor('white')
plt.xticks(ind)
ax.set_xticklabels(labels, rotation = -90)
ax.set_xticks(ind+(width/2))
plt.show()
ax.spines['bottom'].set_position('zero')
ax.set_xticklabels(labels, rotation = -90