how to fix overlap in xlabels using matplotlib - python

Hello all i'm currently trying to plot some data using matplotlib. Unfortunately every time I plot the xlabels overlap each other. How can I go about getting this to space out the labels?Code snippet below. my dict has keys of 'neighborhood' and values of int
#do data visualization here
myList = dict.items()
#myList = sorted(myList)
x, y = zip(*myList)
plt.xlabel("Neighborhoods")
plt.ylabel("# of Public Art")
plt.title("Public Art Distribution in Pittsburgh")
plt.xscale('log', base=3)
plt.plot(x, y)
plt.show()
I'm trying to get the labels to not overlap here
EDIT: thank you to the two suggestions of rotation and to increase figure size. By using these two I was able to get it displaying correctly

import matplotlib.ticker as ticker
ax.xaxis.set_major_locator(ticker.MultipleLocator(5))
You can try to set the frequency to five or more.

Related

How to show all the labels in matplotlib?

I am trying to display a chart using matplotlib. But my labels are so big that they are overlapping each other. I want to show it cleanly no overlapping. How can I do that? I am now using below code:
import matplotlib.pyplot as plt
x = ['jdwdw723#gmail.com' ,'emcast.test10#gmail.com', 'pbChinaTester#clp.com']
y = [10,25,6]
plt.plot(x,y)
plt.xlabel("loginId")
plt.ylabel("times appeared in the data")
plt.title("loginId Graph")
plt.tight_layout()
plt.show()
I tried your example code, and it doesn't seem to be overlapping there. There are many possibilities. One, commonly used, is to rotate the labels.
You can do it like this:
plt.xticks(rotation=45)
There are more ideas in Changing the “tick frequency” on x or y axis in matplotlib? and in reducing number of plot ticks.
I created an example notebook here, feel free to duplicate and play with it.

How do I use mathlibplot.hist with x and y values using bins=40 in Python 3?

I have a large list of data points of x and y values that I need to put into a histogram with 40 bins but mathlibplot.hist is only letting me enter 1 variable with bins. I've tried hist2d as well but it's not very clean. Any help would be appreciated!
As you have data points x and y, you can simply use hist method to plot histogram.
The following code will help you to create a histogram.
plt.hist([x,y],bins=40, histtype='step',fill=True)
plt.show()
The histogram will look like the following:
If you want to change the style or give it title and labels, you can do it. Here is another histogram with unfilled bars.
If you still face any problem, let me know then.
Maybe you can make use of matplotlib library to solve your purpose:
It will be like imposing 2 histograms on top of each other.
In the below code, I am trying to plot a histograms of y_train and predicted(X_train) in the same space.
You can modify the variables as per your requirement.
import matplotlib.pyplot as plt
plt.hist(y_train, stacked=True,bins=40, label='Actual', alpha=0.5)
plt.hist(regressor.predict(X_train),bins=40, stacked=True, label='Predicted', alpha=0.5)
plt.legend(loc='best')
plt.show()
Hope this helps!

violinplot style histogram in plot

I have a plot with several data points that I would like to keep as it is.
One of the data point is 'better', because it does not only come with a value but also a probability assigned to it.
I would like to show that probability by plotting the normal data points, and for the one with the measured PDF show a violinplot.
So far I've done this with a scatter plot over plotted which looks somewhat like this (in an MWE):
import numpy as np
import matplotlib.pyplot as plt
def plot():
x = np.linspace(0,20,20)
data = x + np.random.rand(len(x))
y = 2*x
histo = np.array([1,2,3,10,20,10,3,1])
y_better = np.array([9.5,9.8,10,11.5,12,13,15,16])
ax = plt.subplot()
ax.plot(x,data,'o')
ax.scatter(np.ones_like(histo)*x[10],y_better,c=histo,norm=matplotlib.colors.LogNorm(),s=100)
plt.show()
plot()
which looks like this:
While this works, and transports the message - but doesn't look too cool.
Following the suggestion by #jadsq, I discovered violinplots, which look exactly like what I want!
I now have the problem that the violinplot function assumes data and then conveniently draws the PDF. In my case I already have a measured PDF (which is what I want to plot). How could I make a plot that looks like the violin plot, but with my PDF (so without the estimation)?
To me it looks like a way of indicating error bars on your point so you could maybe try representing it with a box plot.
Regarding the color map: just add cmap='inferno' in the scatter call,like so :
ax.scatter(np.ones_like(histo)*x[10],y_better,c=histo,norm=matplotlib.colors.LogNorm(),s=100,cmap='inferno')

manipulate tick labels on a colorbar

I want to access the tick labels on my matplotlib colobar, so that I can manipulate them.
My starting labels may be [-2,-1,0,1,2] for example.
I have used:
locs,oldlabels = plt.xticks()
newlabels = ['a','b','c','d','e']
plt.xticks(locs, newlabels)
This works. But I don't want to manually write in the new labels. I want to access the oldlabels, so that I can have the newlabels as say [2*(-2), 2*(-1), 2*0, 2*1, 2*2].
I just don't know how to 'get at' the oldlabels. I googled everything and tried lots of things, but I'm doing something fundamentally wrong.
I tried to print oldlabels[0], but I get Text(0,0,u'\u22122.0').
EDIT:
I'm currently doing:
new_labels = [1,2,3,4,5,6,7,8,9]
colorbarname.ax.set_xticklabels(new_labels)
which works. But I want to set them as 2 x their old value. How can I do this automatically? I need to extract the old label values, multiply by (say) 2, update the axis labels with the new values.
If your data is not confined to [0,1], I'd recommend using a norm when you pass the data to the colormap instead of changing the data and relabeling the colorbar: http://matplotlib.org/api/cm_api.html?highlight=norm%20colormap#matplotlib.cm.ScalarMappable.norm
However, you can relabel the colorbar by manipulating the underlying axis directly:
import numpy as np
import pylab as plt
A = np.random.random((10,10))
plt.subplot(121)
plt.imshow(A,interpolation='nearest')
cb = plt.colorbar()
oldlabels = cb.ax.get_yticklabels()
print(map(lambda x: x.get_text(),oldlabels))
newlabels = map(lambda x: str(2 * float(x.get_text())), oldlabels)
print(newlabels)
cb.ax.set_yticklabels(newlabels)
plt.show()
oh, and now I find the matplotlib gallery example, nearly the same: http://matplotlib.org/examples/pylab_examples/colorbar_tick_labelling_demo.html

How do I change the density of x-ticks of a pandas time series plot?

I am trying to generate a smaller figure visualising a pandas time series. The automatically-generated x-ticks, however, do not adapt to the new size and result in overlapping ticks. I am wondering how can I adapt the frequency of the x-ticks? E.g. for this example:
figsize(4, 2)
num = 3000
X = linspace(0, 100, num=num)
dense_ts = pd.DataFrame(sin(X) + 0.1 * np.random.randn(num),
pd.date_range('2014-01-1', periods=num, freq='min'))
dense_ts.plot()
The figure that I get is:
I am able to work around this problem using the Matplotlib date plotting, but it is not a very elegant solution - the code requires me to specify all the output formatting on a per-case basis.
figsize(4, 2)
from matplotlib import dates
fig, ax = plt.subplots()
ax.plot_date(dense_ts.index.to_pydatetime(), dense_ts, 'b-')
ax.xaxis.set_minor_locator(dates.HourLocator(byhour=range(24),
interval=12))
ax.xaxis.set_minor_formatter(dates.DateFormatter('%H:%m'))
ax.xaxis.set_major_locator(dates.WeekdayLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('\n\n%a\n%Y'))
plt.show()
I'm wondering if there is a way to solve this issue using the pandas plotting module or maybe by setting some axes object properties? I tried playing with the ax.freq object, but couldn't really achieve anything.
You can pass a list of x axis values you want displayed in your dense_ts.plot()
dense_ts.plot(xticks=['10:01','22:01'...])
Another example for clarity
df = pd.DataFrame(np.random.randn(10,3))
Plot without specifying xticks
df.plot(legend=False)
Plot with xticks argument
df.plot(xticks=[2,4,6,8],legend=False)

Categories

Resources