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)
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
Try setting the min and max of your x and y axes.
Related
I'm trying to create a plot which has y axis exactly same with this :
And I'm in this situation and everything is ok until this point:
When i try this lines:
ax.set_yticks(range(20,67,10))
ax.set_yticklabels(['20','30','40','50','60'])
My graph is becoming this:
I couldn't understand how to set locations' of yticks properly.
Have you tried the following?
import numpy as np
ticks = np.linspace(20, 60, 5)
ax.set_yticks(ticks)
ax.set_yticklabels([str(int(x)) for x in ticks])
If you want numbers on the axes, make sure you plot numbers, not strings. The axis labels would then adjust automatically as desired, or you may use set_yticks and set_yticklabels as in the question.
I am trying to plot a data and function with matplotlib 2.0 under python 2.7.
The x values of the function are evolving with time and the x is first decreasing to a certain value, than increasing again.
If the function is plotted against time, it shows function like this plot of data against time
I need the same x axis evolution for plotting against real x values. Unfortunately as the x values are the same for both parts before and after, both values are mixed together. This gives me the wrong data plot:
In this example it means I need the x-axis to start on value 2.4 and decrease to 1.0 than again increase to 2.4. I swear I found before that this is possible, but unfortunately I can't find a trace about that again.
A matplotlib axis is by default linearly increasing. More importantly, there must be an injective mapping of the number line to the axis units. So changing the data range is not really an option (at least when the aim is to keep things simple).
It would hence be good to keep the original numbers and only change the ticks and ticklabels on the axis. E.g. you could use a FuncFormatter to map the original numbers to
np.abs(x-tp)+tp
where tp would be the turning point.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker
x = np.linspace(-10,20,151)
y = np.exp(-(x-5)**2/19.)
plt.plot(x,y)
tp = 5
fmt = lambda x,pos:"{:g}".format(np.abs(x-tp)+tp)
plt.gca().xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(fmt))
plt.show()
One option would be to use two axes, and plot your two timespans separately on each axes.
for instance, if you have the following data:
myX = np.linspace(1,2.4,100)
myY1 = -1*myX
myY2 = -0.5*myX-0.5
plt.plot(myX,myY, c='b')
plt.plot(myX,myY2, c='g')
you can instead create two subplots with a shared y-axis and no space between the two axes, plot each time span independently, and finally, adjust the limits of one of your x-axis to reverse the order of the points
fig, (ax1,ax2) = plt.subplots(1,2, gridspec_kw={'wspace':0}, sharey=True)
ax1.plot(myX,myY1, c='b')
ax2.plot(myX,myY2, c='g')
ax1.set_xlim((2.4,1))
ax2.set_xlim((1,2.4))
Let's look at a swarmplot, made with Python 3.5 and Seaborn on some data (which is stored in a pandas dataframe df with column lables stored in another class. This does not matter for now, just look at the plot):
ax = sns.swarmplot(x=self.dte.label_temperature, y=self.dte.label_current, hue=self.dte.label_voltage, data = df)
Now the data is more readable if plotted in log scale on the y-axis because it goes over some decades.
So let's change the scaling to logarithmic:
ax.set_yscale("log")
ax.set_ylim(bottom = 5*10**-10)
Well I have a problem with the gaps in the swarms. I guess they are there because they have been there when the plot is created with a linear axis in mind and the dots should not overlap there. But now they look kind of strange and there is enough space to from 4 equal looking swarms.
My question is: How can I force seaborn to recalculate the position of the dots to create better looking swarms?
mwaskom hinted to me in the comments how to solve this.
It is even stated in the swamplot doku:
Note that arranging the points properly requires an accurate transformation between data and point coordinates. This means that non-default axis limits should be set before drawing the swarm plot.
Setting an existing axis to log-scale and use this for the plot:
fig = plt.figure() # create figure
rect = 0,0,1,1 # create an rectangle for the new axis
log_ax = fig.add_axes(rect) # create a new axis (or use an existing one)
log_ax.set_yscale("log") # log first
sns.swarmplot(x=self.dte.label_temperature, y=self.dte.label_current, hue=self.dte.label_voltage, data = df, ax = log_ax)
This yields in the correct and desired plotting behaviour:
It's easier to ask this with a figure. At the moment i obtain the following boxplot graph using matplotlib:
Is there a way to obtain a figure like that, but with each box in a position coherent with the corresponding x-axis number (like in a normal scatter plot, but with boxes instead of points)?
At the moment the numbers on the x-axis are added by means of the labels= argument.
You need to specify the positions argument to the boxplot constructor.
from matplotlib import pyplot as plt
plt.boxplot([[1,4],[2,5],[3,6]], positions=[2,4,5.5])
By default it uses the values [1, 2, ..., n] but you can specify a different x position for each bar and the xticks will be updated automatically.
I'm plotting a meshgrid with pyplot.pcolormesh, and I want to customize the ticklabels on the colorbar. I set a list of tick positions, and provide a list of ticklabels, which should match the tick positions, but I don't know ahead of time which ticks will actually be included, since I don't know the max and the min of the data. The problem is that the first ticklabel I provide is always used at the first visible tick, regardless of whether that is the first tick in my list or not.
Working example:
import matplotlib.pyplot as plt
import numpy as np
a = np.arange(1,10).reshape(3,3)
m = plt.pcolormesh(a)
c = plt.colorbar(m)
c.set_ticks(np.arange(11))
c.set_ticklabels(np.arange(11))
plt.savefig('mesh.png')
This code produces the image below, and the problem here is that the darkest blue is labled 0, while the value in that cell is actually 1, and similarly all the other labels are shifted by 1.
Is this a bug or a feature, and if it's a feature, how can I make sure the labels will match in an elegant manner? I guess I manage with some tests on the data and trying to figure out which tick will be the first visible and so on, but that doesn't seem very pythonic.
Its a feature, because you are setting the ticklabels yourself (with the wrong labels). Its best always trying to avoid setting the ticklabels manually, unless there is no other way.
If you remove this line, the labels will show up correctly:
c.set_ticklabels(np.arange(11))
To improve readability you could also consider normalizing the colors so they become discrete and match specific integer values. But this only works well if the total amount of colors is limited, like in this example.
fig, ax = plt.subplots()
cmap = plt.cm.jet
bounds = np.arange(0.5,10.5,1)
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
m = ax.pcolormesh(a, cmap=cmap, norm=norm)
c = plt.colorbar(m, ticks=bounds-0.5)