Reading the matplotlib documentation, it looks like style is a module of matplotlib (matplotlib.style).
However, I've seen this code:
matplotlib.pyplot.style.use('seaborn')
I don't understand how the style module can be associated with (and called from) the matplotlib.pyplot module.
Matplotlib has a module style. This can be imported like
from matplotlib import style
The above line is also present inside the pyplot.py.
style is thus in the namespace of pyplot and you can do
matplotlib.pyplot.style.use("some_style")
Related
I am getting a weird error when calling any function with matplotlib.
import matplotlib as plt
plt.bar(np.arange(7, [1,3,2,1,4,6,1])
I get this error:
IPython.core.display.Javascript object
IPython.core.display.HTML object
Traceback (most recent call last): self.kernel.session.send(self.kernel.iopub_socket, msg_type,*
**AttributeError: 'NoneType' object has no attribute 'session'**
I have already tried:
upgrade my pip
upgrade matplotlib
installing pyplot (which brings error --> Non-zero exit code (1) --> Could not find a version that satisfies the requirement pyplot (from versions: ) No matching distribution found for pyplot)
My Matplotlib version = 3.3.3
OS --> Windows 10
Nothing seems to work and I am stuck. Do you have any idea why this is?
You are importing wrongly the library.
Usually one use
import matplotlib as mpl
(seldom used) or
import matplotlib.pyplot as plt
You are mixing the two imports/names, and so also the interface. Use the second import I wrote, for plt (pyplot).
Ho to debug? When you gain experience, you will learn that usually the problems are in your code and not on environment. So stop upgrading/reinstalling (often you worse the situation, see this site for examples). So try to debug your code. Check the matplotlib page of bar plots. Copy example, modify little things until you have your expected code. (Usually with such steps you find the error).
When using a notebook in Google Colab, my matplotlib plots have different behaviors whether I import the library pandas-profiling or not.
If I do not import pandas-profiling, the plots are displayed inline by default. But if I import the library, the plots stop being displayed inline.
Workarounds (possible solutions)
Updating the pandas-profiling library before importing it resolves the problem.
Adding %matplotlib inline after importing pandas-profiling resolves the problem.
Reproducing
Run this code in Google Colab to reproduce the problem. Test it with and without importing pandas_profiling. For each test, you will need to terminate the session (Runtime->Manage Sessions->Terminate). Just restarting the runtime is not enough.
import matplotlib.pyplot as plt
# importing the pandas_profiling makes matplotlib
# to stop showing the plot inline
# import pandas_profiling
plt.plot([1, 2], [1, 2])
The expected behavior is to show the plot inline by default, but after importing pandas-profiling, the plots stop being displayed inline.
The real problem
I stumbled upon this problem when my seaborn plotting functions started to break.
For example, consider the following code.
import matplotlib.pyplot as plt
# import pandas_profiling
import seaborn as sns
def plot():
ax = sns.pointplot([1, 2], [1, 2])
print(len(ax.collections))
Now call plot() in two different jupyter cells.
Without pandas-profiling: each function call will print 1.
With pandas-profiling: each function call will add 1 to the previous output.
The problem is in the version of pandas_profiling that is installed by default in Google Colab. The installed version (1.4.1) used to change the matplotlib's backend to agg when imported. The default matplotlib's backend in Colab is module://ipykernel.pylab.backend_inline.
We can see that by running the following before and after importing pandas-profiling:
import matplotlib
matplotlib.get_backend()
This behavior was changed in pull-request #125, which stops changing matplotlib's backend.
Until Google Colab updates the installed version, manually updating it seems to be the best solution. Just run the following in your Colab Notebook:
!pip install --upgrade pandas_profiling
I tried using heatmap as the chart type but I am getting error 17. I am using vue-highcharts and wondering how I can fix this proble. I looked online and it seems that I may have to import it and use it. Can anyone show me how to do that because I can't find any documentation on it?
import HighCharts from 'highcharts'
import heatmap from 'highcharts/modules/heatmap';
heatmap(HighCharts);
These are the import statements for it to work.
I am trying to scale my decision tree to fit notebook but it appears not to scale properly. I have to keep scrolling for a better view. Can I please have some help on how to fix this. Attach is a pic of how it looks like.
from graphviz import Source
from sklearn import tree
from IPython.display import SVG
graph = Source( tree.export_graphviz(dt_classifier, out_file=None, feature_names=X.columns))
SVG(graph.pipe(format='svg'))
Perhaps it's not relevant any more, since this question has been open for about six months now. However, I just stumbled into it, as apparently 83 other readers, and I just crafted my way around this. The easy way is to use the pydot package (pip install pydot), and then add the default size. I have also been using %matplotlib inline so that it displays nicely within the notebook but without using the svg module. With your example:
%matplotlib inline
from graphviz import Source
from sklearn import tree
import pydot
dot_data = tree.export_graphviz(dt_classifier, out_file=None, feature_names=X.columns))
pdot = pydot.graph_from_dot_data(dot_data)
# Access element [0] because graph_from_dot_data actually returns a list of DOT elements.
pdot[0].set_graph_defaults(size = "\"15,15\"")
graph = Source(pdot[0].to_string())
graph
I also added rotate=True to export_graphviz so that it displays in horizontal style, the root of the tree is directly visible, and is easier to follow. Of course, you can play around with size so as to reach something that is acceptable for you.
I made some changes of matplotlib settings and can't figure out how to put it back. it would be great if you can help me out!
original settings
image that i see now
You question lacks relevant information such as exactly what did you change. In any case something like this should solve your problem:
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)
Try this:
import matplotlib.pyplot as plt
print plt.style.available
which will give you the list of available styles on your machine ('classic','seaborn', etc).
From here you can set which existing plot style you want to use with
plt.style.use('classic')