I am trying to plot a couple of line graphs using Matplotlib and the small dashes which marks the center of the xticks are not showing up. Here is a sample plot I found online which has the marks (I circled them).
Now below is my code and the graph. I know it's not related to spines.
Code:
from sklearn.externals import joblib
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import math
sns.set()
sns.set_style("dark")
sns.set_style("white")
plt.figure()
plt.plot([1,2,3,4,5],[10,5,69,38,52],label='test')
plt.xticks([1,2,3,4,5],['apple','orange','grapes','lemon','pear'],ha='right')
plt.xticks(rotation=45)
plt.savefig("test.png", dpi=300)
OS: MacOX High Sierra 10.13, Python: 3.6.0 and no Virtual Environments
You can customise the seaborn styles to add back ticks (there is also a style for ticks). See here for the full details.
Just change your set_style line to have a second parameter which is a dictionary of overrides. In this case it is setting the size of the xticks. That link gives the full details of built-in styles and all the options for overriding.
sns.set_style("white", {"xtick.major.size": 5})
Related
I am trying to plot a histogram with seaborn of multiple data in ndarray format. I am confused as to how to give different colors per column. The color parameter seems to have no effect.
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
d = np.random.random((1000,3))
sns.histplot(d, color='red')
plt.show()
When i change it to
sns.histplot(d, color='blue')
The colors do not change.
The only way to control the color is by using palette, for example:
sns.histplot(d, palette='Blues')
previously i had an error without specifying a color, but it seems to be a problem with a specific machine configuration.
Consider following code (adapted from here):
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import tikzplotlib
r=np.linspace(0,1,11)
theta=np.linspace(0,2*np.pi,11)
R,Theta=np.meshgrid(r,theta)
X,Y=R*np.cos(Theta),R*np.sin(Theta)
Z=R*np.sin(Theta)*np.cos(Theta)
fig=plt.figure(1)
plt.clf()
ax=fig.add_subplot(projection='3d')
ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=cm.inferno,linewidth=0)
ax.set_xlabel('test here')
plt.show()
tikzplotlib.save("testfigure.tex")
which gives following figure :
When I import the file testfigure.tex in a LaTeX file, after having replaced
\begin{axis}[
hide x axis,
hide y axis,
by
\begin{axis}[
width=10cm,
width=10cm,
I obtain this result :
What can I do, in order to have the axis as in the Python figure? Or is this simply not possible with tikzplotlib?
Github Readme of tikzplotlib states 3D plots are not supported (2021).
Tikzplotlib - Github
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
%matplotlib notebook
plt.figure(figsize=(12, 6))
CasData.pivot(index='year', columns='CasualtyNumber', values='People').plot(kind='bar')
plt.title('Casualties per year')
plt.xlabel('Year', fontsize=5)
plt.ylabel('Number of Casualties')
plt.show()
My plot bar graph using matplotlib.pyplot isn't showing.
I don't know why but my bar graph isn't showing. I've tried different ways.
If someone could help me out please. I'd appreciate it. Thank you.
Remove the line %matplotlib notebook.
It is overriding the previous line (these two lines are setting the backend). inline returns static plots, notebook is used for interactivity.
You also do not need the plt.show() line. This is taken care of by the inline backend.
This answer explains more about the backends: https://stackoverflow.com/a/43028034/6709902
I'm not really sure about your code as it seems incomplete but if you're using pivot I assumed you're pulling the data from a ".csv" file.
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib notebook
CasData = pd.read_csv('data.csv')
CasData.pivot_table(index='year', columns='CasualtyNumber', values='People').plot(kind='bar')
plt.title('Casualties per year')
plt.xlabel('Year',fontsize='5')
plt.ylabel('Number of Casualties')
plt.show()
You need to provide the data in order to plot something and I don't
see you providing any.
Trying to create a simple Box Plot using Google Colab for my Intro Python class. It is not appearing as I would like it. You can see my code and output below. I read in a file on NBA statistics, and my box plot would be based on a variable called "SHOT_CLOCK".
So far what I have:
import pandas as pd
from matplotlib import pyplot as plt
df = pd.read_csv('file path')
plt.boxplot(df['SHOT_CLOCK'], vert=False)
plt.title('Box Plot for SHOT_CLOCK')
plt.xlabel('Shot Clock')
plt.show()
Output:
Edit
In your example you are passing a Series object, try this way
plt.figure()
plt.title('Box Plot for SHOT_CLOCK')
plt.xlabel('Shot Clock')
df.boxplot(column='SHOT_CLOCK')
Once you add the following Import to your code it will work:
import matplotlib.pyplot as plt
plt.style.use('classic')
%matplotlib inline
I am plotting values that are of order 10^-8 and I would like my inline plot in jupyter to output the yaxis ticks in that (scientific) notation. I tried :
plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%.1E'))
as well as
plt.gca().yaxis.get_major_formatter().set_powerlimits((0, -10))
and
plt.ticklabel_format(style='sci')
but nothing seems to work. What am I doing wrong? I have the following example:
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import mpld3
mpld3.enable_notebook()
import matplotlib.ticker as mtick
a=5*10**-8
b=3*10**-8
x=np.arange(0,10,0.01)
y=a*x+b
plt.figure(figsize=(12,5))
plt.subplot(1,2,1)
plt.plot(x,y)
# plt.ticklabel_format(style='sci')
# plt.gca().yaxis.get_major_formatter().set_powerlimits((0, -10))
plt.gca().yaxis.set_major_formatter(mtick.FormatStrFormatter('%.0e'))
plt.show()
Any pointers would be helpful as I don't find anything on this, besides ticks format of an axis in matplotlib, enter link description here or Change x axes scale in matplotlib
NOTE: If I comment out the lines with import mpld3 and mpld3.enable_notebook() then it works but cannot interact with the plot... Is there some special treatment of matplotlib when plotting inline in jupyter?
Thanks!
You can use set_yticklabels to have a similar looking output.
ax = plt.gca()
ax.set_yticklabels(['10^-8','2*10^-8','3*10^-8','4*10^-8'])