I am trying to plot the same geospatial data reading this tutorial:
https://www.datacamp.com/community/tutorials/geospatial-data-python
But the legend of her final graph has shwown wind speed in ranges. But when I used the same code mentioned on her tutorial, I could not produce the same legend. Any thing missing in her code? or What is wrong?
The categorical legend entries are provided via PySAL, and seem to need the scheme=<> and legend=True arguments. Perhaps these were set by default in previous versions of the libraries. For me, the following works ok:
fig, ax = plt.subplots(1, figsize=(20, 20))
base = country[country['NAME'].isin(['Alaska','Hawaii']) == False].plot(
ax=ax, color='#3B3C6E')
florence.plot(
ax=ax, column='Wind', marker="<", markersize=10,
cmap='cool', scheme="Quantiles", legend=True)
plt.axis('off')
Unfortunately the markers do not seem to be inherited into the legend, but the colors are the more salient difference anyway.
The geopandas.plot docs indicate 3 supported schemes: Quantiles, Equal_interval, fisher_jenks -- the first one seems to correspond to the datacamp example chart. See also the parameter k which defines the number of classes (5 is default as this example).
Related
I want to plot on a map pie charts representing some products frequency. I use plotly for that. I looked at the different proposals made by the community on the subject and i went to use this solution. The issue is that i do not understand how to reduce/adjust my pie chart size as you will see in the code below.
fig = go.Figure()
# Add trace
fig.add_trace(go.Pie(values=[17674,12279,11963,10384,5858],labels=['Meals','Processed proteins','Bakery','Snacks','Vegetables'], domain_x=(0.1, 0.3), domain_y=(0.2, 0.4))
)
fig.update_traces(showlegend=False,selector=dict(type='pie'))
# Add images
fig.add_layout_image(
dict(
source="path_to_blank_map",
# xref="x",
# yref="y",
x=0,
y=1.2,
sizex=1.8,
sizey=2,
opacity=0.5,
layer="below")
)
# Set templates
fig.update_layout(template="plotly_white",height=800,width=1200)
fig.show()
I tried to find if there is a way to update a trace size within the add_trace or the update_trace method but it seems impossible. The update_layout method increase or decrease size for the whole graph object and not for specific traces.
Any help would be welcome!!! Thanks a lot!
Okay this might be easy even I search on web but could not get it. Basically i want to add the two different labels to my plot and this is my line of code for that
plt.plot(x[:,1],y,'ro',x[:,1],Line_fit,'b',linewidth=0.5,markersize=4,label="training data") # plot the data
plt.legend(loc="upper left")
but I am getting following result in which has same labels for both the plot.as following
Even I tried this
plt.plot(x[:,1],y,'ro',x[:,1],Line_fit,'b',linewidth=0.5,markersize=4,label="training data",label="Linear Regression") # plot the data
but give the error:
SyntaxError: keyword argument repeated
This link guide for the simple way but here plt.plot()had used twice in the accepted answer.My question is how can i do it the same thing in single line code as I did in my code ?
You need two lines. One for the plot, one for the legend.
plt.plot(x[:,1], y, 'ro', x[:,1], Line_fit, 'b', linewidth=0.5, markersize=4)
plt.legend(["training data", "Linear Regression"], loc="upper left")
I'm trying to produce a pdf file (with PdfFile) containing several figures in one page.
The most obvious solution is to use subplots. However, in my case this is not possible because each plot is produced by a different function. For example, there is a function def plotPDF(inputData) that will plot a probability distribution function (PDF), and another function def plotCDF(inputData) that will plot a cumulative distribution function (CDF). My code contains up to 20 different functions that will produce different plots when they are called.
What I want to do is to select some of these plots and produce a pdf file where they are contained in the same page. Following the example of PDF and CDF, I would like to produce a pdf file which contains one page where both plots are next to each other (in a similar way to the subplots).
I have tried to do this with subplots, but I cannot directly call the function within a subplot. That is, the following code wouldn't work:
fig, ax = plt.subplots(nrows=1, ncols=2)
plt.subplot(1, 2, 1)
plotPDF(inputData)
plt.subplot(1, 2, 2)
plotCDF(inputData)
plt.show()
Does anybody know how to solve this issue ? I need to proceed like this because I need the plot functions to be independent for other purposes. Making subplots would mean changing this structure, and it would make the code less versatile.
Thanks in advance !
I don't know if there's a way to do what you are asking, maybe someone else will know...
but
the recommended way to write a plotting function is to pass a reference to an Axes object to the function, and write the function to use that axes to do the plotting.
so in your case:
def plotPDF(data, ax=None):
ax = ax or plt.gca() # if no axes, use current axes
plt.sca(ax) # set axes as current axes (important if you are using the pyplot API)
# the rest of the function goes here
def plotCDF(data, ax=None):
ax = ax or plt.gca()
plt.sca(ax)
(...)
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
plotPDF(inputData, ax=ax1)
plotCDF(inputData, ax=ax2)
plt.show()
Have you read this answer to a similar post? The answers in that post contain many ways to save an image into a pdf file, and only one (the matplotlib backend) requires subplots, as far as I know.
You can also save the files separately as png files, then use LaTeX / Word / another primitive way to arrange them into a pdf, which could be tedious.
Otherwise, could you maybe elaborate why using subplots wouldn't work with your functions? Maybe there is a way to use subplots, but then you'll need to show us the code.
I am facing serious difficulties in retrieving the bounding box of a mpl_toolkits.axes_grid1.anchored_artists "AnchoredSizeBar" object.
So far, all I found was how to insert (create) an AnchoredSizeBar into my figure. But I can't retrieve its position. A relative position in respect to its given axes would also be welcome.
Ultimately, I would like to make a AnchoredSizeBar as Behnam asks in1. But in order to do so, I would need to create a series of AnchoredSizeBars all contiguous one to another in the figure. In order to do so, I would need a function to retrieve their relative positions and apply those into the creation of the other AnchoredSizeBars, creating finally a full scalebar.
This subject is related to other links presented below:
1) How to insert scale bar in a map in matplotlib
2) Add fill_bar argument to AnchoredSizeBar
Here is a snippet code of a desired API for AnchoredSizeBar object extent retrieval.
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
fig, ax = plt.subplots(figsize=(3, 3))
bar0 = AnchoredSizeBar(ax.transData, 0.3, 'unfilled', loc=3, frameon=False,
size_vertical=0.05, fill_bar=False)
ax.add_artist(bar0)
bar0_extent = bar0.get_extent()
fig.show()
I thank you for your time, sincerely yours,
Philipe Leal
To get the extent you can pass in ax.figure.canvas.renderer where ax is the axes to which the object has been added to:
bar0_extent = bar0.get_extent(ax.figure.canvas.renderer)
But I second #ImportanceOfBeingErnest's suggestion for your particular use case. The doc-string for AnchoredSizeBar suggests that methods:
Docstring: An offset box placed according to the legend location
loc. AnchoredOffsetbox has a single child. When multiple children is
needed, use other OffsetBox class to enclose them. By default, the
offset box is anchored against its parent axes. You may explicitly
specify the bbox_to_anchor.
Hope this helps.
I have a number of subplots within a single figure. Each figure plots multiple lines that represent the same thing (represented by color) but in different situations (different subplots). I would like to create a legend at the base of the figure showing what the color of the line means. However, I running into a problem with getting the legend to not overlap the subplots and if I can adjust the axes, getting the legend to save.
I have tried a few different solutions with some help here but have been unable to adapt to subplots. Below is an example code that I am working with.
import numpy as np
import matplotlib.pyplot as plt
m1=1
m2=10
x=np.linspace(0,100,num=101,endpoint=True)
y1m1=m1*x**2
y2m1=m1*x**0.5
y1m2=m2*x**2
y2m2=m2*x**0.5
fig=plt.figure(figsize=(4,4))
ax1=fig.add_subplot(211)
ax1.plot(x,y1m1,'b',label=r'$x^2$')
ax1.plot(x,y2m1,'r',label=r'$\sqrt{x}$')
ax2=fig.add_subplot(212)
ax2.plot(x,y1m2,'b')
ax2.plot(x,y2m2,'r')
fig.legend(loc='lower center',ncol=2)
fig.tight_layout()
fig.savefig('examplefig.png',dpi=300)
plt.show()
My goal is to save the output to a png for a good figure.
This is one way of doing it using the suggestion provided here. The idea is to add the legend at position with respect to a given axis object. In your case, since you want to add the legend at the base, it is preferred you specify the position relative to ax2. Using ncol=2 is a matter of personal choice.
fig=plt.figure(figsize=(4,4))
ax1=fig.add_subplot(211)
l1, = ax1.plot(x,y1m1,'b')
l2, = ax1.plot(x,y2m1,'r')
ax2=fig.add_subplot(212)
ax2.plot(x,y1m2, 'b')
ax2.plot(x,y2m2, 'r')
ax2.legend(handles = [l1,l2] , labels=[r'$x^2$', r'$\sqrt{x}$'],
bbox_to_anchor=(0.7, -0.2), ncol=2)
fig.tight_layout()