xticks don't show in matplotlib - python

I'm trying to plot out a dictionary data with matplotlib in python3.6, macOS.
I want the keys of the dict to be printed as sticks but they are not showing actually.
My code is as below:
import pandas as pd
import glob
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
%matplotlib inline
figure(num=None, figsize=(500, 100), dpi=80, facecolor='w', edgecolor='k')
D = info_dict
x = list(D.keys())
y = list(D.values())
plt.bar(x,y)
plt.xticks(range(len(D)), list(D.values()), rotation='vertical')
plt.margins(0.2)
plt.subplots_adjust(bottom=0.15)
plt.show()
And the plotted one is like this:

Related

Why can't I see labels using pyplot [duplicate]

When I execute the following code, it doesn't produce a plot with a label.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1, 5)
plt.plot(x, x*1.5, label='Normal')
Numpy version is '1.6.2'
Matplotlib version is '1.3.x'
Any ideas as to why this is happening?
You forgot to display the legend:
...
plt.legend(loc='best')
plt.show()

How to create specific plots using Pandas and then store them as PNG files?

So I am trying to create histograms for each specific variable in my dataset and then save it as a PNG file.
My code is as follows:
import pandas as pd
import matplotlib.pyplot as plt
x=combined_databook.groupby('x_1').hist()
x.figure.savefig("x.png")
I keep getting "AttributeError: 'Series' object has no attribute 'figure'"
Use matplotlib to create a figure and axis objects, then tell pandas which axes to plot on using the ax argument. Finally, use matplotlib (or the fig) to save the figure.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Sample Data (3 groups, normally distributed)
df = pd.DataFrame({'gp': np.random.choice(list('abc'), 1000),
'data': np.random.normal(0, 1, 1000)})
fig, ax = plt.subplots()
df.groupby('gp').hist(ax=ax, ec='k', grid=False, bins=20, alpha=0.5)
fig.savefig('your_fig.png', dpi=200)
your_fig.png
Instead of using *.hist() I would use matplotlib.pyplot.hist().
Example :
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
y =[10, 20,30,40,100,200,300,400,1000,2000]
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='$y = Values')
plt.title('my plot')
ax.legend()
plt.show()
fig.savefig('tada.png')

How to remove y axis of a figure with a discontinous x axis?

I am trying to plot a figure (as shown below) with discontinous x axis with the help of brokenaxes module.
https://pypi.org/project/brokenaxes/
But I don't know how to remove y axis in the figure. I tried some approaches from other quesitons, but seems not compatible with brokenaxes module. Anybody can help?
The code is presented here.
import matplotlib.pyplot as plt
from brokenaxes import brokenaxes
import numpy as np
import pandas as pd
fig = plt.figure(figsize=(7,3))
bax = brokenaxes(xlims=((2500, 4000), (600, 1800)), hspace=1000)
p1= pd.read_csv("p1_1.csv", header=None, skiprows=2)
p1=p1.values
p1=p1.transpose()
bax.plot(p1[0], p1[1])
bax.get_xaxis
bax.invert_xaxis()
bax.set_xlabel('Wavenumber ($cm^{-1}$)')
bax.text(0.05, 0.3, "s", fontsize=12)
fig.savefig("1.png", bbox_inches = "tight")
plt.show()
You can access the individual sub-axes create by brokenaxes through the array BrokenAxes.axs[]
for some reason I had to turn the axis invisible and remove the spine as well, but this works:
import matplotlib.pyplot as plt
from brokenaxes import brokenaxes
fig = plt.figure(figsize=(7,3))
bax = brokenaxes(xlims=((2500, 4000), (600, 1800)), hspace=1000)
#p1= pd.read_csv("p1_1.csv", header=None, skiprows=2)
#p1=p1.values
#p1=p1.transpose()
#bax.plot(p1[0], p1[1])
bax.invert_xaxis()
bax.set_xlabel('Wavenumber ($cm^{-1}$)')
bax.text(0.05, 0.3, "s", fontsize=12)
bax.axs[0].get_yaxis().set_visible(False)
bax.axs[0].spines['left'].set_visible(False)
plt.show()
Use the axs propertie of bax:
bax.axs[0].yaxis.set_visible(False)
bax.axs[0].spines['left'].set_visible(False)

Displaying pair plot in Pandas data frame

I am trying to display a pair plot by creating from scatter_matrix in pandas dataframe. This is how the pair plot is created:
# Create dataframe from data in X_train
# Label the columns using the strings in iris_dataset.feature_names
iris_dataframe = pd.DataFrame(X_train, columns=iris_dataset.feature_names)
# Create a scatter matrix from the dataframe, color by y_train
grr = pd.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15), marker='o',
hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)
I want to display the pair plot to look something like this;
I am using Python v3.6 and PyCharm and am not using Jupyter Notebook.
This code worked for me using Python 3.5.2:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn import datasets
iris_dataset = datasets.load_iris()
X = iris_dataset.data
Y = iris_dataset.target
iris_dataframe = pd.DataFrame(X, columns=iris_dataset.feature_names)
# Create a scatter matrix from the dataframe, color by y_train
grr = pd.plotting.scatter_matrix(iris_dataframe, c=Y, figsize=(15, 15), marker='o',
hist_kwds={'bins': 20}, s=60, alpha=.8)
For pandas version < v0.20.0.
Thanks to michael-szczepaniak for pointing out that this API had been deprecated.
grr = pd.scatter_matrix(iris_dataframe, c=Y, figsize=(15, 15), marker='o',
hist_kwds={'bins': 20}, s=60, alpha=.8)
I just had to remove the cmap=mglearn.cm3 piece, because I was not able to make mglearn work. There is a version mismatch issue with sklearn.
To not display the image and save it directly to file you can use this method:
plt.savefig('foo.png')
Also remove
# %matplotlib inline
Just an update to Vikash's excellent answer. The last two lines should now be:
grr = pd.plotting.scatter_matrix(iris_dataframe, c=Y, figsize=(15, 15), marker='o',
hist_kwds={'bins': 20}, s=60, alpha=.8)
The scatter_matrix function has been moved to the plotting package, so the original answer, while correct is now deprecated.
So the complete code would now be:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn import datasets
iris_dataset = datasets.load_iris()
X = iris_dataset.data
Y = iris_dataset.target
iris_dataframe = pd.DataFrame(X, columns=iris_dataset.feature_names)
# create a scatter matrix from the dataframe, color by y_train
grr = pd.plotting.scatter_matrix(iris_dataframe, c=Y, figsize=(15, 15), marker='o',
hist_kwds={'bins': 20}, s=60, alpha=.8)
This is also possible using seaborn:
import seaborn as sns
df = sns.load_dataset("iris")
sns.pairplot(df, hue="species")
I finally know how to do it with PyCharm.
Just import matploblib.plotting as plt instead:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import mglearn
from pandas.plotting import scatter_matrix
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
iris_dataset = load_iris()
X_train,X_test,Y_train,Y_test = train_test_split(iris_dataset['data'],iris_dataset['target'],random_state=0)
iris_dataframe = pd.DataFrame(X_train,columns=iris_dataset.feature_names)
grr = scatter_matrix(iris_dataframe,c = Y_train,figsize = (15,15),marker = 'o',
hist_kwds={'bins':20},s=60,alpha=.8,cmap = mglearn.cm3)
plt.show()
Then it works perfect as below:
first of all use
pip install mglearn
then import the mglearn
the code will be like this...
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import pandas as pd
import mglearn
import matplotlib.pyplot as plt
iris_dataframe=pd.DataFrame(X_train,columns=iris_dataset.feature_names)
grr=pd.scatter_matrix(iris_dataframe,
c=y_train,figsize=(15,15),marker='o',hist_kwds={'bins':20},
s=60,alpha=.8,cmap=mglearn.cm3)
plt.show()

How do you create line segments between two points?

I have this bit of code that plots out the points:
import matplotlib.pyplot as plot
from matplotlib import pyplot
all_data = [[1,10],[2,10],[3,10],[4,10],[5,10],[3,1],[3,2],[3,3],[3,4],[3,5]]
x = []
y = []
for i in xrange(len(all_data)):
x.append(all_data[i][0])
y.append(all_data[i][1])
plot.scatter(x,y)
pyplot.show()
but I want all the possible lines that could be made that looks something like this:
I've tried matplotlib path, but it doesn't work well for me.
This can be optimized but it works:
for point in all_data:
for point2 in all_data:
pyplot.plot([point[0], point2[0]], [point[1], point2[1]])
import matplotlib.pyplot as plt
import itertools
fig=plt.figure()
ax=fig.add_subplot(111)
all_data = [[1,10],[2,10],[3,10],[4,10],[5,10],[3,1],[3,2],[3,3],[3,4],[3,5]]
plt.plot(
*zip(*itertools.chain.from_iterable(itertools.combinations(all_data, 2))),
color = 'brown', marker = 'o')
plt.show()
One other way could be to use matplotlib patches
import matplotlib
import pylab as pl
fig, ax = pl.subplots()
import matplotlib.patches as patches
from matplotlib.path import Path
verts = [(x1,y1), (x2,y2)]
codes = [Path.MOVETO,Path.LINETO]
path = Path(verts, codes)
ax.add_patch(patches.PathPatch(path, color='green', lw=0.5))
using all combinations?
import matplotlib.pyplot as plot
from matplotlib import pyplot
all_data = [[1,10],[2,10],[3,10],[4,10],[5,10],[3,1],[3,2],[3,3],[3,4],[3,5]]
x = []
y = []
for i in combinations(all_data,2):
x.extend(i[0])
y.extend(i[1])
plot.plot(x,y)
pyplot.show()

Categories